gpt4 book ai didi

Python:在循环中对变量执行用户定义的加法

转载 作者:太空宇宙 更新时间:2023-11-04 05:39:48 25 4
gpt4 key购买 nike

这是我正在开发的大型“自动售货机”中的一个模块。不过,我遇到了一些问题。如果他们支付的金额不够($1.75),他们将被要求输入额外的金额。我的部分问题是我不知道我应该做什么操作来改变循环内的赤字。我尝试过的所有事情都导致了错误,例如“预计输入最多 1 个参数,得到 3 个”等等。

selection = 5
loopCount = 0
deposit = 0
cost = 1.75
print("It costs $",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 = deposit - cost
if deposit < cost:
while deficit > 0 and loopCount < 1:
??? = float(input("Please enter an additional $",deficit,"."))
loopCount += 1
if deposit >= cost:
print("Thank you for purchasing item#",selection,". Your change is $",change,".")

最佳答案

这对我有用。请确保增加 loopCount,因为现在它不会循环多次。

selection = 5
loopCount = 0
deposit = 0
cost = 1.75
print("It costs $",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 = deposit - cost
if deposit < cost:
while deficit > 0 and loopCount < 1:
deficit -= float(input("Please enter an additional ${}.".format(deficit)))
loopCount += 1
if deposit >= cost:
print("Thank you for purchasing item#",selection,". Your change is $",change,".")

关于Python:在循环中对变量执行用户定义的加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34323908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com