![](/img/trans.png)
[英]I don't have any errors and my loop just keeps running. What am I doing wrong?
[英]My while loop is keeps looping. What am I doing wrong?
这里的总金额就是用户需要支付的金额。 如果用户支付的金额少于总金额,系统需要减去付款并显示他们的余额并继续循环直到支付完成。 但是,当我运行我的代码时,它会在显示余额的同时继续循环。
print ("Your total amount is:", total_amount)
print ("")
payment = int(input("Please insert your payment: "))
count = 0
while payment != total_amount:
count = total_amount - payment
print ("Your balance:", count)
payment = int(input("Please insert your payment: "))
if payment == total_amount:
print ("Successful")
如果用户在第一次输入时输入总额,程序会输出“成功”吗?
如果用户在第一次输入时没有输入总金额,可能发生的情况是名为 payment 的变量永远不会等于总付款,但计数最终会达到总金额 yes,但是 while 循环没有变量count working 是逻辑语句,它使用 payment 作为其逻辑的变量。 如果有人从商店买了一件东西,总共是 5 美元,而买家有 1 美元的钞票,买家会先把 1 美元放在柜台上,然后是另外 1 美元,依此类推......但是买家不会在柜台上放 1 美元,然后在柜台上放 5 美元,因为那将是 6 美元
您没有在循环中正确更新 payment 或 total_amount 。 我相信这应该有效。
payment = int(input("Please insert your payment: "))
while payment <= total_amount:
diff = total_amount - payment
print("Your balance:", diff)
payment += int(input("Please insert your payment: "))
if payment == total_amount:
print("Successful")
根据您的代码,只有当我在一个 go 中支付正确的金额时,我才能成功支付。 当然这是错误的。 我已经修改并向您的代码添加了一些细节,如下所示:
print("Your total amount is: $%.2f \n" % (total_amount))
count = 0
while count < total_amount:
payment = int(input("Please insert your payment: $"))
count += payment
print ("Your balance: $%.2f" % (total_amount - count))
print("Successful")
if count > total:
print("Your change is $%.2f" % (count - total))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.