循环遇到麻烦并收集用户输入
嗨,我正在从事一个自助服务程序的作业。在收集用户输入时,我很难尝试合并一个时循环。我希望我的代码收集一笔钱(用户输入),并提示用户需要支付多少钱。一旦支付的金额超过了应付的金额,我想继续进行其余的代码。这是我到目前为止所拥有的,但是即使用户付款不足,循环也会在2个输入后停止。
elif paid < TotalCost:
due = TotalCost - paid
while due > 0:
print("You need to pay $",round(due,2)," more.")
morepaid=float(input("Enter the amount paid: $"))
if morepaid>TotalCost:
change= morepaid- TotalCost
print("Change due: $",round(change,2))
print(" ")
print("Thank you for visiting McDonald's")
elif due== 0:
print("Change due: $0.00")
print(" ")
print("Thank you for visiting McDonald's")
else:
due -= morepaid
Hi I'm working on an assignment where I make a self-service program. I'm having trouble trying to incorporate a while loop while collecting user input. I want my code to collect an amount of money (entered by the user) and prompt the user on how much more they need to pay. Once the amount paid exceeds the amount due, I want to continue with the rest of my code. This is what I have so far but the loop stops after 2 inputs, even if the user has not paid enough.
elif paid < TotalCost:
due = TotalCost - paid
while due > 0:
print("You need to pay quot;,round(due,2)," more.")
morepaid=float(input("Enter the amount paid: quot;))
if morepaid>TotalCost:
change= morepaid- TotalCost
print("Change due: quot;,round(change,2))
print(" ")
print("Thank you for visiting McDonald's")
elif due== 0:
print("Change due: $0.00")
print(" ")
print("Thank you for visiting McDonald's")
else:
due -= morepaid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地从
到期
中减去金额,您不需要另一个变量totalpaid
。You can simply subtract the amount paid from
due
, you don't need another variabletotalpaid
.