+ 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
6 Antworten
+ 3
In case with no errors the program will only print an integer.
Output should be a coffee type, not an integer.
+ 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")
+ 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")
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;
}
- 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")