Assignment: Create a python file called “ VendingMachine.py ”. Download “ Vendin
ID: 3712848 • Letter: A
Question
Assignment:
Create a python file called “VendingMachine.py”. Download “VendingMachine.txt” from BB and copy it into “VendingMachine.py. The file contains code defining the two functions, “getCoins” and “calcChange”. It also defines a list, “itemList”, containing the description and price of each item.
Design and implement a program to simulate a vending machine. Submit both the flowchart and the resultant code. The functionality should be as follows:
Using the list of items (itemList), print out what items are available, their price and which button to press. Use button “0” for the first item; button “1” for the second item and button “2” for the third item.
Using “getCoins”, have the customer deposit a group of coins. (Note: “getCoins” returns the amount in pennies.)
Ask the customer to make a selection or enter “9” to get any remaining change.
As long as the remaining change is equal to or greater than 75 cents (the price of the cheapest selection) and the customer has not pressed “9”:
Compare the change remaining to the price of the item as found in “itemList”. If the remaining change is equal to or greater than the price of the selected item, display “Vending:” and the description of the item, then subtract the price of the item from the remaining change. If the remaining change is less than the price of the selected item, display “Not enough money. Try another item”
Print change remaining
If the remaining change is equal to or greater than 75 cents, ask the customer to make another selection
Once the remaining change is less than 75 cents or the customer has selected “9”, use “calcChange” to get a list of coins to refund and print out the number of coins of each coin type.
Be sure to thank the customer for their business !!
Example
Select from the following choices:
Select 0 for Candy price: 1.25
Select 1 for Water price: 0.75
Select 2 for Granola Bar price: 1.0
Enter list of coins : 25,25,25,25,10,10,10,5,1,25
total is $ 1.61
Enter selection or 9 to get change1
Vending Water
total remaining is $ 0.86
Enter selection or 9 to get change2
Not enough money. Try another item.
total remaining is $ 0.86
Enter selection or 9 to get change9
Dispensing change:
0 Dollars
3 Quarters
1 Dimes
0 Niclels
1 Pennies
Thank you for your patronage !!
HERE IS THE “VendingMachine.txt WE MUST USE:
def getCoins():
total=0
coinList=eval(input("Enter list of coins : "))
for coin in coinList:
total += coin
return total
def calcChange(amount):
changeList=[0,0,0,0,0]
changeList[0]=amount//100
amount=amount%100
changeList[1] = amount // 25
amount = amount % 25
changeList[2] = amount // 10
amount = amount % 10
changeList[3] = amount // 5
amount = amount % 5
changeList[4] = amount
return changeList
def main():
itemList = [["Candy", 125], ["Water", 75], ["Granola Bar", 100]]
Explanation / Answer
import time
print ("Welcome to the Python Vending Machine.")
# Asking the user how much money they wish to enter.
number_of_10p = int(input("How many 10 pence coins would you like to insert? "))
while number_of_10p < 0:
number_of_10p = int(input("Please enter a positive number."))
number_of_20p = int(input("How many 20 pence coins would you like to insert? "))
while number_of_20p < 0:
number_of_20p = int(input("Please enter a positive number."))
number_of_50p = int(input("How many 50 pence coins would you like to insert? "))
while number_of_50p < 0:
number_of_50p = int(input("Please enter a positive number."))
number_of_100p = int(input("How many 1 pound coins would you like to insert? "))
while number_of_100p < 0:
number_of_100p = int(input("Please enter a positive number."))
# Creating a variable to store the total amount of money inserted into the vending machine.
change = round(((number_of_10p * 0.10) + (number_of_20p * 0.20) + (number_of_50p * 0.50) + (number_of_100p * 1.00)),2)
# Informing the user how much they have entered in total.
print (" In total you have entered £", change)
time.sleep(2)
# Creating variables for the 5 products and their respective prices.
product_1, product_1_price = "Flake", 0.55
product_2, product_2_price = "Wispa", 0.50
product_3, product_3_price = "Crunchie", 0.65
product_4, product_4_price = "Milky Way", 0.35
product_5, product_5_price = "Boost", 0.65
# Creating variables to track the number of each items bought,
flakes_bought = 0
wispas_bought = 0
crunchies_bought = 0
milky_ways_bought = 0
boosts_bought = 0
# Informing the user of the choices available and the prices that of each item.
print ("There are 5 products available to pick from; ")
time.sleep(2)
print ("Item: {}, Price {} ".format(product_1, product_1_price))
print ("Item: {}, Price {} ".format(product_2, product_2_price))
print ("Item: {}, Price {} ".format(product_3, product_3_price))
print ("Item: {}, Price {} ".format(product_4, product_4_price))
print ("Item: {}, Price {} ".format(product_5, product_5_price))
print ("")
# Asking the user to make a selection.
while change > 0:
customer_choice = input("What would you like to buy? Type N when you are finished ")
if customer_choice == "Flake" or customer_choice == "flake" and change >= product_1_price:
print ("You have chosen a", product_1, "these cost", product_1_price, "each,")
change = round((change - product_1_price),2)
flakes_bought = (flakes_bought + 1)
print ("You have this much money remaining: £", change)
elif customer_choice == "Wispa" or customer_choice == "wispa" and change >= product_2_price:
print ("You have chosen a", product_2, "these cost", product_2_price, "each,")
change = round((change - product_2_price),2)
wispas_bought = (wispas_bought + 1)
print ("You have this much money remaining: £", change)
elif customer_choice == "Crunchie" or customer_choice == "crunchie" and change >= product_3_price:
print ("You have chosen a", product_3, "these cost", product_3_price, "each,")
change = round((change - product_3_price),2)
crunchies_bought = (crunchies_bought + 1)
print ("You have this much money remaining: £", change)
elif customer_choice == "Milky Way" or customer_choice == "milky way" and change >= product_4_price:
print ("You have chosen a", product_4, "these cost", product_4_price, "each,")
change = round((change - product_4_price),2)
milky_ways_bought = (milky_ways_bought + 1)
print ("You have this much money remaining: £", change)
elif customer_choice == "Boost" or customer_choice == "boost" and change >= product_5_price:
print ("You have chosen a", product_5, "these cost", product_5_price, "each,")
change = round((change - product_5_price),2)
boosts_bought = (boosts_bought + 1)
print ("You have this much money remaining: £", change)
elif customer_choice == "N" or customer_choice == "n":
print (" Here is your transaction details: ")
print ("You purchased: ")
print (product_1, "x", flakes_bought)
print (product_2, "x", wispas_bought)
print (product_3, "x", crunchies_bought)
print (product_4, "x", milky_ways_bought)
print (product_5, "x", boosts_bought)
print ("You have £", change, "remaining.")
break
elif change <= 0:
print ("You have run out of money.")
print (" Here is your transaction details: ")
print ("You purchased: ")
print (product_1, "x", flakes_bought)
print (product_2, "x", wispas_bought)
print (product_3, "x", crunchies_bought)
print (product_4, "x", milky_ways_bought)
print (product_5, "x", boosts_bought)
break
else:
print ("There has been an error or you may not have enough credit.")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.