Python: 在循环中对变量执行用户定义的加法
Python: Performing user-defined addition to a variable in a loop
这是我正在研究的更大 "vending machine" 的一部分的模块。不过,我 运行 遇到了一些问题。如果他们支付的金额不够($1.75),他们将被要求输入额外的金额。我的部分问题是我不知道我应该做什么操作来改变循环内的赤字。我尝试过的所有事情都导致了 "input expected at most 1 arguments, got 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 .50, .50 for [=11=].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,".")
假设您不想在他们支付足够的钱之前打破循环,您可以这样做:
while abs(deficit) < cost:
added = float(input("Please enter an additional $",deficit,"."))
deficit = cost - deposit - added
abs 函数将输出绝对值,因此您可以将其直接与成本进行比较。
不过需要注意的是,您可能需要对输入进行错误检查。
此外,如果您确实希望能够为用户提供一种机制来打破循环,您可以添加一个布尔值 true 或 false(而不是循环计数),它附加到一些其他输入,如 "Do you want to insert more money? y or n"
这对我有用。请务必增加 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 .50, .50 for [=10=].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,".")
这是我正在研究的更大 "vending machine" 的一部分的模块。不过,我 运行 遇到了一些问题。如果他们支付的金额不够($1.75),他们将被要求输入额外的金额。我的部分问题是我不知道我应该做什么操作来改变循环内的赤字。我尝试过的所有事情都导致了 "input expected at most 1 arguments, got 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 .50, .50 for [=11=].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,".")
假设您不想在他们支付足够的钱之前打破循环,您可以这样做:
while abs(deficit) < cost:
added = float(input("Please enter an additional $",deficit,"."))
deficit = cost - deposit - added
abs 函数将输出绝对值,因此您可以将其直接与成本进行比较。
不过需要注意的是,您可能需要对输入进行错误检查。 此外,如果您确实希望能够为用户提供一种机制来打破循环,您可以添加一个布尔值 true 或 false(而不是循环计数),它附加到一些其他输入,如 "Do you want to insert more money? y or n"
这对我有用。请务必增加 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 .50, .50 for [=10=].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,".")