0

Making Coffee Question, need help!

'''Need help 3 test pass but 1 fail ''' coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: #your code goes here if choice in range(0,5): print (coffee[choice]) except: #and her print("Invalid") finally: #and finally here print("Have a good day")

9th Jan 2022, 12:10 PM
Sẩm Lâm Bảo Quý
Sẩm Lâm Bảo Quý - avatar
7 Respuestas
+ 3
You need to print "Invalid number". And I think this part is not necessary: if choice in range(0,5): I mean you have try: So you don't need to make sure that the number is in range. How I solved it: coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: #your code goes here print(coffee[choice]) except: #and here print("Invalid number") finally: #and finally here print("Have a good day")
9th Jan 2022, 12:16 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Sẩm Lâm Bảo Quý Because of this line: if choice in range(0,5): Input is 8 if choice in range(0,5) gets false and nothing will happen. The try part will not work so the except part is not executed.
9th Jan 2022, 12:26 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Sẩm Lâm Bảo Quý Just use except: You need to catch all exceptions. Because the input could also be a string which leads to an other exception than a number out of range.
9th Jan 2022, 12:28 PM
Denise Roßberg
Denise Roßberg - avatar
0
I get it, but still 3 pass, 1 fail Choice input = 8 Except not working
9th Jan 2022, 12:21 PM
Sẩm Lâm Bảo Quý
Sẩm Lâm Bảo Quý - avatar
0
I have still google this things for a hour... jeez
9th Jan 2022, 12:22 PM
Sẩm Lâm Bảo Quý
Sẩm Lâm Bảo Quý - avatar
0
#here the code pass...jeez I did it coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: #your code goes her print (coffee[choice]) except IndexError: #and her print("Invalid number") finally: #and finally here print("Have a good day")
9th Jan 2022, 12:25 PM
Sẩm Lâm Bảo Quý
Sẩm Lâm Bảo Quý - avatar
0
except IndexError
9th Jan 2022, 12:26 PM
Sẩm Lâm Bảo Quý
Sẩm Lâm Bảo Quý - avatar