ValueError won’t work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

ValueError won’t work

In the following code, I can’t get the ValurError to produce the desired output “Item not found”. I’ve tried everything that I can think of. The rest of the program works fine, just not that. I’d appreciate any feedback you can give. menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda'] order = int(input()) try: print(menu[order]) except ValueError: print("Item not found") except IndexError: print("Item not found") else: print("Thanks for your order")

7th Mar 2022, 8:58 PM
Gina
3 Answers
+ 6
A ValueError I believe occurs when you try to convert to int what cannot be interpreted as number. But that code line is outside the try-block. Put the try before order = int(input()).
7th Mar 2022, 9:03 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 5
# Put the try before order = int(input()) https://code.sololearn.com/c3nawUtZndaI
7th Mar 2022, 9:14 PM
SoloProg
SoloProg - avatar
+ 3
Thank you Ani Jona 🕊 and SoloProg that instantly solved my problem, thank you so much. Such a simple fix to something that had me chasing my tail for hours (aussie saying for running around in circles). Have an amazing week 😊 Thank you again.
7th Mar 2022, 9:36 PM
Gina