In: Computer Science
Write a program that simulates a vending machine. The machine holds six snack items labeled them 1 through 6. The program should initially display a menu of items along with their prices:
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00
The program then should ask the user to enter the item to purchase along with a sum of money. If the money is enough to buy the item, your program should display the name of item purchased along with the change owed to the user (if any). If the money inserted is insufficient, then your program should say so and let the user know how much additional money is needed. Your program must display the money amounts using the dollar sign and two decimal places after the decimal point.
Please note that your program must validate the input given by the user:
Here are a few sample runs:
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 Enter your choice of item: 1 Enter money to purchase item: 10 Thanks for buying Roasted Almonds. Your change is $8.75.
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 Enter your choice of item: 6 Enter money to purchase item: 1.50 You are $0.50 short.
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 Enter your choice of item: 9 Invalid item choice.
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 Enter your choice of item: A Value entered was not a number.
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 Enter your choice of item: 2 Enter money to purchase item: abc Value entered was not a number.
Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 Enter your choice of item: 4 Enter money to purchase item: -2.00 Amount of money cannot be a negative value.
Notes:
How your program will be graded:
Please use python 3
# declaring variables
choice=""
almonds=1.25
pretzels=1.75
che_Gum=0.90
mints=0.75
chocolate_bar=1.50
cookies=2.00
while choice!=0:
print("""
Vending Machine
\n\n
1. Roasted Almonds --> $1.25
2. Pretzels --> $1.75
3. Chewing Gum --> $0.90
4. Mints --> $0.75
5. Chocolate bar --> $1.50
6. Cookies --> $2.00""")
try:
choice=int(input("Enter your choice of item:"))
if(choice >= 1 and choice <=6):
if choice==1:
money=int(input("Enter money to purchase item:"))
if money<0:
print("Amount of money cannot be a negative value.")
elif money >= almonds:
change= money - almonds
print("Thanks for buying Roasted Almonds\n Your change is $",change)
else:
print("you are",(almonds-money)," short")
elif choice==2:
money=int(input("Enter money to purchase item:"))
if money<0:
print("Amount of money cannot be a negative value.")
elif money >= pretzels:
change= money - pretzels
print("Thanks for buying pretzels \n Your change is $",change)
else:
print("you are",(pretzels-money)," short")
elif choice==3:
money=int(input("Enter money to purchase item:"))
if money<0:
print("Amount of money cannot be a negative value.")
elif money >= che_Gum:
change= money - che_Gum
print("Thanks for buying Chewing Gum \n Your change is $",change)
else:
print("you are",(che_Gum-money)," short")
elif choice==4:
money=int(input("Enter money to purchase item:"))
if money<0:
print("Amount of money cannot be a negative value.")
elif money >= mints:
change= money - mints
print("Thanks for buying Mints \n Your change is $",change)
else:
print("you are",(mints-money)," short")
elif choice==5:
money=int(input("Enter money to purchase item:"))
if money<0:
print("Amount of money cannot be a negative value.")
elif money >= chocolate_bar:
change= money - chocolate_bar
print("Thanks for buying Chocolate bar \n Your change is $",change)
else:
print("you are",(chocolate_bar-money)," short")
elif choice==6:
money=int(input("Enter money to purchase item:"))
if money<0:
print("Amount of money cannot be a negative value.")
elif money >= cookies:
change= money - cookies
print("Thanks for buying cookies \n Your change is $",change)
else:
print("you are",(cookies-money)," short")
else:
print("Enter valid choice 0-EXIT")
except:
print("An exception occurred")
output Screen