Try, except, finally in Python problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Try, except, finally in Python problem

Any help with what’s wrong with my code?? Each coffee option has its own number, starting with 0. Write a program that will take a number from the customer as input from the customer and serve the corresponding coffee type. If the customer enters a number that is out of the accepted range, the program should output "Invalid number". Regardless of coffee option result, the program should output "Have a good day" at the end. Sample Input 7 Sample Output Invalid number Have a good day

13th Oct 2020, 8:00 PM
Greezy gree
Greezy gree - avatar
6 Answers
+ 3
In case with no errors the program will only print an integer. Output should be a coffee type, not an integer.
13th Oct 2020, 8:25 PM
Seb TheS
Seb TheS - avatar
+ 1
heres what i tried: try: #your code goes here coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input(coffee)) print(str(choice)) except: #and here print("Invalid number") finally: #and finally here print("Have a good day")
13th Oct 2020, 8:02 PM
Greezy gree
Greezy gree - avatar
+ 1
coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: print(coffee[choice]) except: print("Invalid number") finally: print("Have a good day")
8th May 2021, 4:49 PM
Pradeep Rawat
Pradeep Rawat - avatar
13th Oct 2020, 8:24 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
0
where i am wrong ? #include <iostream> using namespace std; int main() { int choice = 0; cin >> choice; /*1 - Latte 2 - Americano 3 - Espresso 4 - Cappuccino 5 - Macchiato */ //your code goes here switch(choice){ case 1: cout<<"Latte"; break; case 2: cout<<"Americano"; break; case 3: cout<<"Expresso"; break; case 4: cout<<"Cappuccino"; break; case 5: cout<<"Macchiato"; break; default: cout<<"nothing"; } return 0; }
27th Jun 2021, 10:25 AM
Chinmay Anand
Chinmay Anand - avatar
- 1
#Here is what I tried and worked : remember to do indentation well by skipping 4 spaces( selecting space on keyboard 4 times) try: coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input(coffee)) print(str(choice)) except: print("Invalid number") finally: print("Have a good day")
3rd Apr 2021, 2:28 PM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar