Exeption Handling | Sololearn: Learn to code for FREE!

-3

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

3/29/2021 1:50:20 AM

Kathiravan P

8 Answers

New Answer

+3

Where is your attempts ??

+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")

+1

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")

+1

#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

0

Wait i will send u now!

0

#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; }

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!")

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; }