укажите мне на ошибку которую не замечаю цикл, while
a = "if you want to buy ticket, please enter how old are you: "
a += "Enter 'quit' when you are finished! "
while True:
age = input(a)
if a == 'quit':
break
elif age > 3:
print('price for ticket is free!')
elif 3 < age < 12:
print('price for ticket is 15')
elif age > 12:
print('price for ticket is 20')
Я понимаю, что надо где то надо добавить int(), но не получается у меня! Благодарен за любую помощь!
Ответы (3 шт):
Автор решения: Sergey Gornostaev
→ Ссылка
Вам нужно поместить в переменную age не строку, а числовое значение, тогда вам будут доступны операции сравнения.
while True:
input_string = input(a)
if input_string == 'quit':
break
if not input_string.isnumeric():
print('Age must be a number!')
age = int(input_string)
if age > 3:
print('price for ticket is free!')
elif 3 < age < 12:
print('price for ticket is 15')
elif age > 12:
print('price for ticket is 20')
Автор решения: S. Nick
→ Ссылка
к вас неправильное условие elif age > 3:
while True:
input_string = input("\nif you want to buy ticket, please enter how old are you ('quit' - Выход): ")
if input_string == 'quit':
break
if not input_string.isnumeric():
print('Age must be a number!')
age = int(input_string)
if age <= 3: # <----
print('цена за билет бесплатно!!')
elif 3 < age < 12:
print('price for ticket is 15')
elif age >= 12:
print('price for ticket is 20')
Автор решения: n1tr0xs
→ Ссылка
С минимальными правками кода (в местах сравнения с числами "добавляем" int()):
a = "if you want to buy ticket, please enter how old are you: "
a += "Enter 'quit' when you are finished! "
while True:
age = input(a)
if a == 'quit':
break
elif int(age) > 3:
print('price for ticket is free!')
elif 3 < int(age) < 12:
print('price for ticket is 15')
elif int(age) > 12:
print('price for ticket is 20')