В этом цикле я задаю пользователю серию вопросов и присваиваю каждый ответ переменной, которая будет использоваться позже в моем скрипте.
Я также выполняю некоторую обработку ошибок, если пользователь не вводит целое число в качестве ответа, но не знает, как заставить его повторить соответствующий вопрос, если пользователь вводит неверный ввод.
questions = ['Please enter your gross annual salary: ', 'Please enter the annual amount of any bonus or commision that appears on your paylip. If not applicable enter 0: ', 'Please enter the annual amount of any benefits in kind that appear on your payslip. If not applicable enter 0: ', 'Please enter your annual total tax credit: ', 'Please enter your annual total pension contribution. If not applicable enter 0: ', 'Do you have any other non tax deductable income? (ie. Holiday purchase schemes etc.) If not applicable enter 0: ']
variables = ['salary', 'bonus', 'bik', 'tax_credits', 'pension contributions', 'other_non_tax_deductibles']
for question, variable in zip(questions, variables):
try:
variable = float(input({question}))
except ValueError:
print("Please enter a number")
else:
print(variable)
Подробнее здесь: https://stackoverflow.com/questions/786 ... rong-input
Как повторить вопрос в Python, если пользователь вводит неправильный ввод? [дубликат] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение