+ 1
Trying to solve chef's kiss in intermediate python
Here is the code i ran: menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda'] menu=input() try: print(menu) except (TypeError , ValueError): print("Item not foumd") else: print("Thanks for your order") I am stuck at this point
10 odpowiedzi
+ 3
use
try:
n=int(input())
// if the input is valid it won't raise exception .if input isn't valid it will raise error .
print(menu[n])
// if n>=length of menu it will raise exception otherwise it will print the order
And use only except: to catch all types of error.
Don't use menu for variable again as menu is already a list.
+ 1
No i meant it
menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda']
try:
n=int(input())
print(menu[n])
except :
print("Item not found")
else:
print("Thanks for your order")
+ 1
I Have used:
...
except(ValueError, TypeError, IndexError):
...
And it works!
+ 1
This worked for me
menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda']
#your code goes here
Ui= (input())
try:
print(menu[int(Ui)])
except(TypeError, ValueError,IndexError):
print("Item not found")
else:
print("Thanks for your order")
+ 1
menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda']
try:
order=int(input())
print(menu[order])
print("Thanks for your order")
except (TypeError , ValueError, IndexError):
print("Item not found")
0
Is this what you meant
menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda']
try:
n=int(input())
print(menu[n])
except (TypeError , ValueError):
print("Item not foumd")
except (TypeError , ValueError ):
print("Thanks for your order")
0
When i tried this an error popped up which was
Bad except clauses order(empty clause should always appear last)
0
I am not finding any error here(my code).
0
Thanks it has worked
0
menu = ['Fries', 'Sandwich', 'Cheeseburger', 'Coffee', 'Soda']
menu_index = input()
try:
print(menu[int(menu_index)])
except (TypeError, ValueError):
print("Item not found")
else:
print("Thanks for your order")