I'm building a application that calculates your daily energy expenditure. For this I have 2 functions which will use the information given in the inputs.
Now, the problem is when an user does not provide an answer on integer, the input has to re-ask the question untill it's valid. For this, I have a loop for every input I've made, but I'd like to have it in 1 loop so I can ask the question at the end of the loop if they want to continue or end the calculator.
Below is my code (it's in Dutch)
while True:
try:
leeftijd = int(input("Wat is uw leeftijd in jaren ? "))
break
except ValueError:
print("Vul het opnieuw in !")
while True:
try:
gewicht = int(input("Wat is uw gewicht in kilogrammen ? "))
break
except ValueError:
print("Vul het opnieuw in !")
while True:
try:
lengte = int(input("Wat is uw lengte in centimeters ? "))
break
except ValueError:
print("Vul het opnieuw in !")
geslacht = input("Wat is uw geslacht (m/v) ? ")
while True:
try:
beweging = int(input("Hoeveel wandelt u per dag (minuten) ? "))
break
except ValueError:
print("Vul het opnieuw in !")
I hope this is clear enough to understand. Thanks in advance!