OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I repeat question in python if user enters wrong input? [duplicate]

  • Thread starter Thread starter DeirdreRodgers
  • Start date Start date
D

DeirdreRodgers

Guest
Ι am asking user a series of questions in this loop and assigning each answer to a variable to be used later in my script.

I'm also doing some error handling if the user doesn't enter an integer as response but not sure how to get it to repeat the relevant question if the user enters invalid input.

Code:
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)
<p>Ι am asking user a series of questions in this loop and assigning each answer to a variable to be used later in my script.</p>
<p>I'm also doing some error handling if the user doesn't enter an integer as response but not sure how to get it to repeat the relevant question if the user enters invalid input.</p>
<pre><code>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)
</code></pre>
 
Top