Exeption Handling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Exeption Handling

Exeption Handling A coffee vending machine makes 5 types of coffee: coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] PY 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

29th Mar 2021, 1:50 AM
Kathiravan P
Kathiravan P - avatar
17 Answers
+ 5
#correct code try: coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) print(coffee[choice]) except IndexError: print('Invalid number') finally: print('Have a good day') #and finally here
17th Jul 2021, 4:32 AM
Hugues Rodrigue Tha Andela
Hugues Rodrigue Tha Andela - avatar
+ 2
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, 3:26 PM
Alok Jha
Alok Jha - avatar
+ 2
#include <iostream> using namespace std; int main() { int choice = 0; cin >> choice; switch (choice){ case 1: cout << "Latte" << endl; break; case 2: cout << "Americano" << endl; break; case 3: cout << "Espresso" << endl; break; case 4: cout << "Cappuccino" << endl; break; case 5: cout << "Macchiato" << endl; break; } return 0; }
5th Oct 2021, 10:17 PM
Caroline Y
Caroline Y - avatar
+ 1
Where is your attempts ??
29th Mar 2021, 2:39 AM
TOLUENE
TOLUENE - avatar
+ 1
#include<iostream> using namespace std; int main() { //Printing Menu cout << "*****Menu***** \n"; //Printing Menu items cout << "1- Latte \n2 - Americano \n3 - Espresso \n4 - Cappuccino \n5 - Macchiato \n"; //A variable to store user choice int choice = 0; //prompt to enter choice cout << "Enter your choice: "; //reading input from user cin >> choice; //A switch statement switch (choice) { //if choice is 1 case 1: cout << "Latte"; //Printing Coffee name Latte break; //terminating case using break statement //if choice is 2 case 2: cout << "Americano:"; //Printing Coffee name Americano break; //terminating case using break statement //if choice is 3 case 3: cout << "Espresso:"; //Printing Coffee name Espresso break; //terminating case using break statement //if case is 4 case 4: cout << "Cappucciono:"; //Printing Coffee name Cappucciono break; //terminating case using break statement //if case is 5 case 5: cout << "Macchiato:"; //Printing Coffee name Macchiato break; //terminating case using break statement } return 0; }
24th Jul 2021, 5:09 AM
Ricardo M Delgado
Ricardo M Delgado - avatar
0
Wait i will send u now!
29th Mar 2021, 3:06 AM
Kathiravan P
Kathiravan P - avatar
0
It's very hard...I tried this but it doesn't work at all coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] choice = int(input()) try: #your code goes here choice = list(range(0,4)) print(coffee) except IndexError: print("Invalid number") #and here finally: #and finally here print("Have a good day") I imagined it should return the error if the number was out of the range 0,4 but it doesn't. Also, I don't know how to tell the program to print the corresponding coffee\choice value (for ex if choice=2, print "Espresso")
20th Apr 2021, 10:45 AM
Simone Bucci
Simone Bucci - avatar
0
coffee = ["Café Latte", "Caffe Americano", "Espresso", "Cappuccino", "Macchiato"] try: choice = int(input("Please choose your coffee ")) served_coffee=coffee[choice] print("You have selected "+served_coffee+". Enjoy!") except IndexError: print("Please select valid coffee type") finally: print("Have a nice day!")
7th Aug 2021, 11:40 AM
Vahram Grigoryan
Vahram Grigoryan - avatar
0
#include <iostream> using namespace std; int main() { int CoffeeNo; cin >> CoffeeNo; if (CoffeeNo == 1) { cout << "Latte" << endl; } if (CoffeeNo == 2) { cout << "Americano" << endl; } if (CoffeeNo == 3) { cout << "Espresso" << endl; } if (CoffeeNo == 4) { cout << "Cappuccino" << endl; } if (CoffeeNo == 5) { cout << "Macchiato" << endl; } return 0; } Good Luck
25th Jan 2022, 11:42 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
#include <iostream> using namespace std; int main() { int CoffeeNo; cin >> CoffeeNo; if (CoffeeNo == 1) { cout << "Latte" << endl; } if (CoffeeNo == 2) { cout << "Americano" << endl; } if (CoffeeNo == 3) { cout << "Espresso" << endl; } if (CoffeeNo == 4) { cout << "Cappuccino" << endl; } if (CoffeeNo == 5) { cout << "Macchiato" << endl; } return 0; }
23rd Oct 2022, 12:27 PM
Abdoulie Samateh
0
#include <iostream> using namespace std; int main() { int distance = 0; //your code goes here for(int i=1; i<=5; i++){ distance=i*40; cout<<distance<<endl; } return 0; }
11th Dec 2022, 3:27 AM
BERNALYN REOVOCA AGUELO
0
cout << "Latte" << endl; break; case 2: cout << "Americano" << endl; break; case 3: cout << "Espresso" << endl; break; case 4: cout << "Cappuccino" << endl; break; case 5: cout << "Macchiato" << endl; break; }
19th Dec 2022, 12:10 PM
Ivan Gama
Ivan Gama - avatar
0
/*zarif jorayev*/ #include <iostream> using namespace std; int main() { int choice = 0; cin >> choice; switch (choice){ case 1: cout << "Latte" << endl; break; case 2: cout << "Americano" << endl; break; case 3: cout << "Espresso" << endl; break; case 4: cout << "Cappuccino" << endl; break; case 5: cout << "Macchiato" << endl; break; } return 0; }
2nd Jan 2023, 7:38 AM
ZARIF JORAYEV
ZARIF JORAYEV - avatar
0
Solution on JavaScript.. let choice = parseInt(readLine(), 10); /* 1 => Americano 2 => Espresso 3 => Cappuccino */ //your code goes here switch (choice){ case 1: console.log("Americano"); break; case 2: console.log("Espresso"); break; case 3: console.log("Cappuccino"); break; default: console.log("Unknown") }
30th Sep 2023, 5:55 PM
Mehedi Hasan
Mehedi Hasan - avatar
0
#include <iostream> using namespace std; int main() { int choice; cin >> choice; // Coffee Types// switch(choice){ case 1: std::cout<<"Espresso"<<std::endl; break; case 2: std::cout<<"Americano"<<std::endl; break; case 3: std::cout<<"Cappucino"<<std::endl; break; case 4: std::cout<<"Latte"<<std::endl; } return 0; }
26th Jan 2024, 12:22 PM
Harley Nemesis
0
#include <iostream> using namespace std; int main (){ int coffeeSelected; cout << "Enter the coffee that you want:"; cout << "(1)Cafe Latte, (2)Caffe Americano, (3)Espresso, (4)Cappucino, (5)Macchiato\n"; cin >> coffeeSelected; switch (coffeeSelected){ case 1: cout << "You have ordered Cafe Latte\nThank you for buying and have a nice day!!"; break; case 2: cout << "You have ordered Caffe Americano\nThank you for buying and have a nice day!!"; break; case 3: cout << "You have ordered Espresso\nThank you for buying and have a nice day!!"; break; case 4: cout << "You have ordered Cappucino\nThank you for buying and have a nice day!!"; break; case 5: cout << "You have ordered a Macchiato\nThank you for buying and have a nice day!!"; break; default: cout << "Invalid Number\nHave a good day!!"; return 0; } }
22nd Mar 2024, 6:32 AM
LAURENCE CHRISTOPHER TORMIS
LAURENCE CHRISTOPHER TORMIS - avatar
- 1
#include <iostream> using namespace std; int main() { int CoffeeNo; cin >> CoffeeNo; if (CoffeeNo == 1) { cout << "Latte" << endl; } if (CoffeeNo == 2) { cout << "Americano" << endl; } if (CoffeeNo == 3) { cout << "Espresso" << endl; } if (CoffeeNo == 4) { cout << "Cappuccino" << endl; } if (CoffeeNo == 5) { cout << "Macchiato" << endl; } return 0; }
27th Aug 2021, 5:37 PM
Angelos Joseph