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

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!)
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
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.
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,...
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.
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
write cplus programs for exercises Drink Machine Simulator Create a class that simulates and manages a...
write cplus programs for exercises Drink Machine Simulator Create a class that simulates and manages a soft drink machine. Information on each drink type should be stored in a structure that has data members to hold the drink name, the drink price, and the number of drinks of that type currently in the machine. The class should have an array of five of these structures, initialized with the following data. Drink Name      Cost   Number in Machine Cola       1.00  ...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT