问题描述
我正在创建一个简单的更改计算器。但是我不确定为什么我的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)