ValueError wonā€™t work | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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