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")
3 Respuestas
+ 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()).
+ 5
# Put the try before order = int(input())
https://code.sololearn.com/c3nawUtZndaI
+ 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.