Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Recover From Errors (Solution)

00:00 Here we are. Let’s give it a go. To start off by asking the user to input an integer, I’m going to say users_integer = input("Enter an integer: ").

00:19 This is going to be a string so far, right? But I do want it to be an integer. So what I can generally do is I can convert this to an integer, so I’m going to say int(), use the int() function here to convert this to an integer.

00:33 But now if the user inputs something that isn’t an integer, then the program’s going to break. Let’s try it out. I already saved this so I can run it. I’m going to say F5.

00:42 I enter an integer, so if I enter an integer here, 3, then no problem. But let’s try it again. If I enter something that can’t be converted to an integer, like e, then I get this traceback ValueError because Python can’t convert the e to an integer.

01:00 So this is the error that I want to catch and do something instead of quitting the program like this. So, what I’ll do is I’ll wrap this inside of a tryexcept block.

01:10 I’m going to say try to convert the user’s input to an integer, and then I want to print it, users_integer. And now I’m going say except I run into this ValueError, and then I want to do this again, right? So I want to collect another input.

01:31 So, an easier way to do this is wrap the whole thing inside of a loop. So I can say while True:

01:40 try to collect and convert the users_input to an integer. But if I run into a ValueError, then I’m just going to tell them to try again, "Please try again!"

01:53 And in this case, I don’t have to do anything. So now I’m repeating the process until the user has entered an integer. Let me just move all of this up there.

02:03 But now I still need to get out of this infinite loop somehow. So, for this, you can use the break keyword that you’ve practiced before, and we put it into the condition where you actually achieved what you want to achieve, right?

02:15 So in this case, I’m going to say break if we did not run into a ValueError, if it worked successfully to convert the user’s input to an integer and then print it back out to them, then we’re done with the program. Then I want to break out of this infinite loop.

02:30 Otherwise, if we keep running into this ValueError, then we are going to just keep looping back to the beginning and trying again. Give it a go. Save this, try it out, enter an integer.

02:43 I’m going to say not an integer, try again.

02:48 Such a polite program too. And then if I add an integer, then it prints it back out and the program quits, and we never run into an exception. Great!

03:01 And here’s that solution: while True, try to collect the user input and print it out. In this case, you’re doing the integer conversion, not right on the input, but then afterwards, which comes down to the same because it’s both still in the same part of the try except block. So, it’ll still throw the ValueError and then catch it down here, and otherwise the solution is the same.

03:23 Did you solve the challenge differently? Then post it in the comments.

MarkYoung on Sept. 29, 2023

Could ask the user to enter a float to test the program too. Just an idea.

Anonymous on Feb. 14, 2024

Here is an alternative solution by using built-in string methods

while True:
    number = input('Enter a number: ')
    if number.isdecimal():
        number = int(number)
        print(f'You entered: {number}')

    print('You entered an invalid number. Try again!\n')

Become a Member to join the conversation.