0

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)
2

3 Answers 3

1

The problem is that there is no exception handling for your second input.

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:
        # if an exception raised here it propagates
        travel_score = int(input("This was not a valid input please try again"))


print ("User travels per year:", travel_score)

The best way to handle this is to put an informative message back to the user if their input is invalid and allow the loop to return to the beginning and re-prompt that way:

# there is no need to instantiate the travel_score variable
while True:
    try:
        travel_score = int(input("How many times per year do you travel? Please give an integer number"))
    except ValueError:
        print("This was not a valid input please try again")
    else:
        break  # <-- if the user inputs a valid score, this will break the input loop

print ("User travels per year:", travel_score)
Sign up to request clarification or add additional context in comments.

Comments

0

@Luca Bezerras answer is good, but you can get it a bit more compact:

travel_score = input("How many times per year do you travel? Please give an integer number: ")

while type(travel_score) is not int:    
    try:
        travel_score = int(travel_score)
    except ValueError:
        travel_score = input("This was not a valid input please try again: ")


print ("User travels per year:", travel_score)

1 Comment

Indeed, nice refactor :P
0

The problem is that once you thrown the ValueError exception, it is caught in the except block, but then if it is thrown again there are no more excepts to catch these new errors. The solution is to convert the answer only in the try block, not immediately after the user input is given.

Try this:

travel_score = 0
is_int = False
answer = input("How many times per year do you travel? Please give an integer number: ")

while not is_int:    
    try:
        answer = int(answer)
        is_int = True
        travel_score = answer
    except ValueError:
        answer = input("This was not a valid input please try again: ")


print ("User travels per year:", travel_score)

2 Comments

@SuperShoot No, it won't, as the cast to int is done in the try block only.
@Malou Sure, glad to be of assistance! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.