I'm a Python beginner and tried to use try
and except
for the first time. I'm asking the user for an integer value but instead of ending the program if the user enters for example a string, I would like to ask the user again and again until an integer is given.
At the moment the user is only asked once to give another answer if he gives a string but if he gives a wrong input again, the program stops.
Below an example of what I mean.
I had a look through similar questions on Stackoverflow but I couldn't fix it with any of the suggestions.
travel_score = 0
while True:
try:
travel_score = int(input("How many times per year do you travel? Please give an integer number"))
except ValueError:
travel_score = int(input("This was not a valid input please try again"))
print ("User travels per year:", travel_score)