Handling user input in Python is essential, but it can also be a source of potential errors if not handled properly. In this tutorial, we'll cover how to take user input safely and handle various scenarios using exception handling. We'll illustrate this with a simple calculator program.
First, let's import the sys
module for system-specific parameters and functions. Then, we'll initialize a variable total
to keep track of the sum of numbers entered by the user.
import sys
total = 0
Next, we'll create a loop to continuously prompt the user for input until they enter 0
to exit the program.
while True:
user_input = input("Enter a number: ")
if user_input == '0':
print("The total is:", total)
sys.exit() # Exit the program
else:
try:
total += int(user_input)
except ValueError:
print("Invalid input! Please enter a valid number.")
In this loop:
- We prompt the user to enter a number using the
input()
function. - If the user enters
0
, we print the total and exit the program usingsys.exit()
. - Otherwise, we attempt to convert the user input to an integer using
int(user_input)
. - If the conversion is successful, we add the integer to the total.
- If the conversion fails due to a
ValueError
, we catch the exception and print an error message.
Let's run through the code:
import sys
total = 0
while True:
user_input = input("Enter a number: ")
if user_input == '0':
print("The total is:", total)
sys.exit() # Exit the program
else:
try:
total += int(user_input)
except ValueError:
print("Invalid input! Please enter a valid number.")
Now, let's test the program:
Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: abc
Invalid input! Please enter a valid number.
Enter a number: 0
The total is: 60
As you can see, the program handles both valid and invalid inputs gracefully. It continues to execute even if the user enters invalid input, preventing the program from crashing. This is achieved through the use of exception handling, specifically catching ValueError
exceptions when converting user input to an integer.
By implementing proper input validation and exception handling, we can create robust programs that gracefully handle unexpected user input and errors. This approach enhances the user experience and ensures the reliability of our applications.