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()
您好。我正在研究自动售货机程序。传递给“付费”功能的变量表现得很糟糕。例如,如果您输入的金额不足并且要求额外输入,则会询问项目的成本,而不是成本 - 存款(赤字)。它也不会正确计算更改。例如,如果我选择第1项(1.75)并支付2美元,而不是给我正确的更改量,它会给我这个:
“感谢您购买第1项。您的更改为-1.75美元。”
我假设付费功能有问题,但我不知道是什么。我已经在这里捡了几个小时。在项目所需的额外资金数额不正确以及返回的错误金额之间肯定存在关联。这个循环有问题吗?缩进?请原谅我,如果它真的很简单 - 我只编程了几个月,还有很多需要学习的东西。
答案 0 :(得分:2)
你以错误的顺序计算东西。在以下代码中:
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
最初总是等于cost
,因为您根据用户输入更改deposit
,但不要重新计算deficit
(由...计算) deposit
已初始化为0
},因此deficit
仍然等于cost
,并且由于deposit
从未使用过,因此它会&#34; eat&#34;最初的存款不足,而不是归功于你。阅读deficit
后,您需要计算deposit
,而不是之前。您还需要检查赤字是> 0
,而不是>= 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:
代码可能会使用很多其他清理工具,但这就是导致问题的原因。