#python #function #while-loop #calculator
#python #функция #во время цикла #калькулятор
Вопрос:
Я создаю простой калькулятор изменений. Однако я не уверен, почему мой цикл 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)
Спасибо!
Комментарии:
1. Это должно быть
while total > 100 or total <= 0:
. Спасибо!2. Абсолютно! Спасибо! Хорошее и простое исправление.
Ответ №1:
Вы должны изменить «и» в вашем условном обозначении while на «или», потому что число не может быть больше 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)
Ответ №2:
Вам просто нужно изменить «и» на «или», и вы решите свою проблему.
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)