+ 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
17 Réponses
+ 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
+ 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")
+ 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;
}
+ 1
Where is your attempts ??
+ 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;
}
0
Wait i will send u now!
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")
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;
}
Good Luck
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;
}
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;
}
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;
}
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;
}
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")
}
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;
}
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;
}
}
- 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;
}