python - Python:使用 While 输入金额
问题描述
初学者在这里。我正在尝试构建一个循环,其中结帐显示的总金额必须超过 0 美元。例如,如果我从 450$ 开始,则代码有效。但是如果我以 -12 开头,它会再次询问我(这就是我想要的),但是如果我输入 450 作为第三篇文章;第一个条件继续运行。这是为什么?提前致谢。
amount = input("What's the total amount of the bill ? :")
value = float(amount)
while (value < 0):
print("Please enter an amount higher than 0$ !")
amount = input("What's the total amount of the bill ? :")
else:
print("Total amount of the bill:{0}".format(value))
解决方案
您忘记更新变量“值”,因此即使在输入 450(值仍为 -12)后,您的 while 循环条件仍然为真。您也可以在同一行中将输入转换为浮点数,这样就不需要“数量”变量
value = float(input("What's the total amount of the bill ? :"))
while (value < 0):
print("Please enter an amount higher than 0$ !")
value = float(input("What's the total amount of the bill ? :"))
else:
print("Total amount of the bill:{0}".format(value))
推荐阅读
- rest - 如何将 PubSub Rest API 与 Google 服务帐户一起使用?
- javascript - 我通过 Parent.But this.prop 将 msg 从 Child1 传递给 Child2
- c# - 如何缓存程序集以供以后编译?[罗斯林]
- python - Pyinstaller - 将 excel 文件捆绑到 onefile exe
- javascript - 损坏的图像图标和类型错误:comments.map 不是函数
- python-3.x - 图像上对象之间的距离
- javascript - 编辑器之前需要的制表符表上下文单击事件
- python - 如何编辑附加数据?
- python - 转换为熊猫日期时间格式时的数据框列导致 NaT
- firebase - 通过身份验证来限制对 Firebase 的访问是否更好?