Handle Invalid User Input #
In the previous lesson, we learned about the try-except block, a powerful construct in Python for handling errors and exceptions that might occur during the execution of a program.
Our current code will break when the user gives non-numeric input because the int() function will raise a ValueError exception if the input is not a valid integer. When the exception is raised, the program will stop with an error message, and the user will not be able to continue with the guessing game.
Here's an example of what happens when the user enters non-numeric input:
❯ python game.py
Guess the number between 1 and 100: abc
Traceback (most recent call last):
File "game.py", line 7, in <module>
guess = int(input("Guess the number between 1 and 100: "))
ValueError: invalid literal for int() with base 10: 'abc'
To handle this error, we can use a try-except block to catch the ValueError exception and handle it gracefully by informing the user that they need to enter a valid integer.
We will add a try-catch block to our code before incrementing the variable attempts as below:
while True:
guess = input("Guess the number between 1 and 100: ")
try:
guess = int(guess)
except ValueError:
print("Invalid input! Please enter a number.")
continue
attempts += 1
if guess == number:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
elif guess < number:
print("Too low. Try again.")
elif guess > number:
print("Too high. Try again.")
In this updated version of the code, we've added a try-except block around the data type conversion statement. If the user enters a non-numeric value, such as a letter, the int() function will raise a ValueError. The except block will then catch the error and print a message telling the user that their input was invalid. The continue statement will then return the program to the start of the loop, so the user can try again.
> python game.py
Guess the number between 1 and 100: abc
Invalid input! Please enter a number.
Guess the number between 1 and 100:
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules