如何解决具有while循环验证的简单更改计算器
我正在创建一个简单的更改计算器。但是我不确定为什么我的while循环不检查用户输入。我希望该程序只接受1到99之间的数字。
total = int(input('How much change do you need? '))
while total > 100 and total <= 0:
print('The change must be between 1 cent and 99 cents.')
total = int(input('How much change do you need? '))
def change(total):
print(total//25,'Quarters')
total = total%25
print(total//10,'Dimes')
total = total%10
print(total//5,'Nickels')
total = total%5
print(total//1,'Pennies')
change(total)
谢谢!
解决方法
您必须将条件中的“和”更改为“或”,因为一个数字不能同时大于100和小于1。
total = int(input('How much change do you need? '))
while total > 100 or total <= 0:
print('The change must be between 1 cent and 99 cents.')
total = int(input('How much change do you need? '))
def change(total):
print(total//25,'Quarters')
total = total%25
print(total//10,'Dimes')
total = total%10
print(total//5,'Nickels')
total = total%5
print(total//1,'Pennies')
change(total)
,
您只需将“ and”更改为“ or”,即可解决您的问题。
total = int(input('How much change do you need? '))
while total > 100 or total <= 0:
print('The change must be between 1 cent and 99 cents.')
total = int(input('How much change do you need? '))
def change(total):
print(total//25,'Pennies')
change(total)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。