def pay(cost, selection,):
deposit = 0
deficit = cost - deposit
change = deposit - cost
print("That item will cost you $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for $1.50, .50 for $0.50, etc.):\n--\n"))
if deposit < cost:
while deficit >= cost:
deficit -= float(input("Please enter an additional $" + str(deficit) + "."))
if deficit > cost:
print("Your change is $",change,".")
print("Thank you for purchasing item#",selection,". Your change is $",change,".")
returnyn = input("Would you like to make another purchase? Answer with a y or n\n")
if returnyn == "Y" or returnyn == "y":
return
if deposit == cost:
print("Thank you for purchasing item#",selection,".")
returnyn = input("Would you like to make another purchase? Answer with a y or n\n")
if returnyn == "Y" or returnyn == "y":
return
if deposit > cost:
print("Thank you for purchasing item#",selection,". Your change is $",change,".")
returnyn = input("Would you like to make another purchase? Answer with a y or n\n")
if returnyn == "Y" or returnyn == "y":
return
else:
exit()
def main():
cost = 0
selection = 0
loopCount = 0
flag = 0
print("--- Welcome to the HOWE CO. VENDOTRON ---\n --- Please make a selection below ---\n")
while loopCount < 3 or flag == 0:
loopCount +=1
if loopCount == 1:
print("You have three transactions left.")
if loopCount == 2:
print("You have two transactions left.")
if loopCount == 3:
print("You have one transaction left.")
if loopCount == 4:
print(("Thank you for your business!"))
break
print ("Item#\tCost")
print("1)\t\t$1.75\n2)\t\t$.75\n3)\t\t$.90\n4)\t\t$.75\n5)\t\t$1.25\n6)\t\t$.75\n7)\t\tExit transaction\n-----\n")
selection = int(input("Please enter the item number:\n"))
if selection > 6 or selection < 1:
print("Invalid input.")
if selection == 1:
cost = 1.75
x = pay(cost,selection,)
if selection == 2:
cost = 0.75
x = pay(cost,selection,)
if selection == 3:
cost = 0.90
x = pay(cost,selection,)
if selection == 4:
cost = 0.75
x = pay(cost,selection,)
if selection == 5:
cost = 1.25
x = pay(cost,selection,)
if selection == 6:
cost = 0.75
x = pay(cost,selection,)
if selection == 7:
print(("Thank you for your business!"))
break
main()
Hello. I am working on a vending machine program. The variables being passed into the "pay" function are acting wonky. For example, if you enter an insufficient amount of money and it requests for additional input, it asks for the cost of the item, not the cost - deposit (deficit.) It also doesn't compute the change correctly either. For example, if I were to choose item 1 (which is 1.75) and pay 2 dollars, instead of giving me the correct amount of change, it gives me this:
"Thank you for purchasing item# 1 . Your change is $ -1.75 ."
I am assuming that there is something wrong with the pay function, but I cannot tell what. I have been picking away at this for hours. There must definitely be a connection between the incorrect amount of additional money needed for the item and incorrect amount of change given back. Is it something wrong with the loops? Indentation? Forgive me if it is something really simple -- I've only been programming for a few months and still have a lot to learn.
You're calculating stuff in the wrong order. In the following code:
deposit = 0
deficit = cost - deposit
change = deposit - cost
print("That item will cost you $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for $1.50, .50 for $0.50, etc.):\n--\n"))
if deposit < cost:
while deficit >= cost:
deficit
is always initially equal to cost
, because you change deposit
based on user input, but don't recalculate deficit
(which was computed from a deposit
initialized to 0
), so deficit
remains equal to cost
, and since deposit
is never used, it will "eat" the initial insufficient deposit and not credit you for it. You need to calculate deficit
after reading in deposit
, not before. You also need to check that the deficit is > 0
, not >= cost
, because you're trying to eliminate the shortfall, not make the shortfall smaller than the cost.
print("That item will cost you $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for $1.50, .50 for $0.50, etc.):\n--\n"))
deficit = cost - deposit
change = -deficit
if deficit > 0:
while deficit > 0:
The code could use a lot of other cleanups, but that's what's causing the problem described.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.