Question

In: Computer Science

Write a program that simulates a vending machine. The machine holds six snack items labeled them...

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:

  • The item to purchase an the amount of money are numeric values (e.g. int and float)
  • If the user enters an invalid choice for an item to purchase, your program should alert the user of the error.
  • The program should not accept negative values for the sum of money.

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:

  • The purpose of this problem is to practice using conditional branching and the try/except clause. Do not use loops.
  • Please make sure to submit a well-written program. Good identifier names, useful comments, and spacing will be some of the criteria that will be used when grading this assignment.
  • This assignment can be and must be solved using only the materials that have been discussed in class. Do not look for alternative methods that have not been covered as part of this course.

How your program will be graded:

  • correctness: it performs the conversion correctly: 40%
  • complies with requirements: correct use of conditional branching: 40%
  • code style: good variable names, comments, proper indentation and spacing, : 20%

Please use python 3

Solutions

Expert Solution

# 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


Related Solutions

3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
Write a java console application,. It simulates the vending machine and ask two questions. When you...
Write a java console application,. It simulates the vending machine and ask two questions. When you run your code, it will do following: Computer output: What item you want? User input: Soda If user input is Soda Computer output: How many cans do you want? User input:            3 Computer output: Please pay $3.00. END The vending machine has 3 items for sale: Soda the price is $1.50/can. Computer should ask “How many cans do you want?” Chips, the price is $1.20/bag....
Write a program that determines the change to be dispensed from a vending machine. An item...
Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and 1 dollar, in 5-cents increments (25, 30, 35, . . . 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. (Explain with algorithms if you can please!)
Given a snack vending machine, assume the machine accepts bills and coins, after customer input money...
Given a snack vending machine, assume the machine accepts bills and coins, after customer input money and select items, the machine can calculate and dispose snack. a. Finish a class diagram of the snack vending machine using correct UML notations. b. Convert the class diagram above to Java code.
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this program, your code can have user defined functions (but not required), the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Requirements: There is a customer with the following credential and can only access the system if the Access Code is correct: • Name: Peter Parker, Access Code: 2222 • When the program...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
Java - Create a program that simulates a slot machine. When the program runs, it should...
Java - Create a program that simulates a slot machine. When the program runs, it should do the following: - Ask the user to enter the amount of money he or she wants to enter into the slot machine. - Instead of displaying images, have the program randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars (To select a word, the program can generate a random number in the range of 0 through 5. If...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT