+ 3

Can anyone explain this

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 I finished the challenge with if and else : coffee = {1:"Café Latte", 2:"Caffe Americano", 3:"Espresso",4:"Cappuccino", 5:"Macchiato"} choice = int(input ()) if choice == 0: print(coffee [0]) elif choice == 1: print(coffee [1]) elif choice == 2: print(coffee [2]) elif choice == 3: print(coffee [3]) elif choice == 4: print(coffee [4]) else: print ("Invalid number") print("Have a good day") But what is required is that the solver of the problem asks me to solve it using try, expact and the finally How can i do it ??

10th Jan 2021, 4:08 PM
XLK
XLK - avatar
10 ответов
+ 18
#Try this 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")
10th Jan 2021, 4:21 PM
Simba
Simba - avatar
+ 6
《 Nicko12 》 Thanks for trying, but your code failed which contains if but the Simba simba code has succeeded Thank you all for your participation
10th Jan 2021, 4:25 PM
XLK
XLK - avatar
+ 4
coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: print(coffee[choice]) """ If choice > 4 means IndexError therefore "except" statement will execute """ except: print ("Invalid number") finally: print ("Have a good day")
10th Jan 2021, 4:20 PM
noteve
noteve - avatar
+ 3
Xluuk Oh that, that is a comment or a docstring, not part of the code and not executed, that is just an explanation. Anyway, You're welcome ☺
10th Jan 2021, 4:26 PM
noteve
noteve - avatar
+ 2
I try this coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: #your code goes here choise > [4] except: #and here print ("invalid number") finally: #and finally here print ("Have a good day") But it is valid
10th Jan 2021, 4:17 PM
XLK
XLK - avatar
+ 2
《 Nicko12 》 Yes i know The thing that your code was missing was the print But thanks for your interest and for trying to help
10th Jan 2021, 4:29 PM
XLK
XLK - avatar
+ 1
try: # get the coffee here except: # invalid number finally: # have a good day
10th Jan 2021, 4:14 PM
noteve
noteve - avatar
0
#Please find my solution below and give me your thoughts, Thx :) try: coffee_type = int(input()) if 0 <= coffee_type < 5: print("Your order has registered") else: print("Invalid number, please enter a number between 0 - 4") except: print("Invalid number, please enter a number, not a character") finally: print("Have a good day")
9th Aug 2022, 8:46 AM
Sait Nasif
0
int main(){ int coffee; cout << "Choose from 1 - 4 Coffee: "; cin >> coffee; switch (coffee) { case 1: cout << "Mocha"; break; case 2: cout << "Arabica"; break; case 3: cout << "Latte"; break; case 4: cout << "Espresso"; break; default: cout << "Invalid Output"; } cout << "\nHave a Good Day" << endl; return 0;
22nd Aug 2022, 7:18 AM
the Monu
the Monu - avatar
- 2
Hope you understand my text! #include <iostream> using namespace std; int main() { int num; cin >> num; switch (num) {case 1: cout << "Arabica"<< endl; cout << "Have a nice day" << endl; break; case 2: cout << "Black coffee"<< endl; cout << "Have a nice day" << endl; break; case 3: cout << "Robusta"<< endl; cout << "Have a nice day" << endl; break; case 4: cout << "Latte" << endl; cout << "Have a nice day" << endl; break;} if (num>4){cout << "Invalid" << endl; cout << "Have a nice day" << endl;} }
21st Sep 2021, 5:58 AM
sai akshagn
sai akshagn - avatar