banner

© 2004-21 ict4u.net

Display List Item

Use an index number to display a list item
Uses some elementary validation

# Display an the element at a given index number
# Includes elementary validation
listFruit = ["Orange", "Apple", "Pear", "Mango"]
numItem = int(input("Enter the index number to display: "))

# now get the index number of the last item in the fruit list

# method 1
lastIndex = listFruit.index(listFruit[-1])

# method 2
lastIndex = len(listFruit) - 1

if numItem > lastIndex or numItem < 0:
	print("Number must be between 0 and {} ".format(lastIndex))
else:
	print("Index number {} is {}".format(numItem, listFruit[numItem]))