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")
7 Réponses
+ 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")
+ 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.
+ 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.
0
I get it, but still 3 pass, 1 fail
Choice input = 8
Except not working
0
I have still google this things for a hour... jeez
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")
0
except IndexError