vendredi 15 novembre 2019

How to create an if statement that will notify the user that the index chosen is higher than the max index in a tuple?

I have a tuple with 4 items, meaning the highest index is 3. I am trying to create an if statement that will tell the user an item doesn't exist if the index they chose is higher than the max index in the tuple (in this case, more than 3). In the screenshot, this is currently what I have. It obviously doesn't work.

inventory = ("sword,",
             "armor",
             "shield",
             "healing potion")

max_index = max(len(x) for x in inventory)

index = int(input("\nEnter the index number for an item in inventory: "))
if index > max_index:
    print("This index does not exist.")
else:
    print("At index", index, "is", inventory[index])

Aucun commentaire:

Enregistrer un commentaire