In this tutorial, we will walk through the process of accepting only numbers as input in Python.
This is a common requirement in many programming scenarios where we want to ensure that the user enters only valid numerical input. We will accomplish this by using Python’s built-in functions and error-handling techniques.
Step 1: Use the input() function to get user input
The first step is to use the input()
function to get input from the user. This function reads a line from input, converts it to a string, and returns it without the trailing newline.
1 |
user_input = input("Enter a number: ") |
Step 2: Convert the user input to a number using int() or float()
Next, we need to convert the string input to a numerical value. We can use the int()
function if we want to accept only integers, or the float()
function if we want to accept decimal numbers as well.
1 2 3 4 |
try: number = int(user_input) except ValueError: print("Please enter a valid integer.") |
1 2 3 4 |
try: number = float(user_input) except ValueError: print("Please enter a valid number.") |
Step 3: Handle exceptions for invalid input
As you can see from the code snippet above, we are using the try
and except
statements to handle the possible exceptions (i.e., errors) that may occur during the conversion process. If the user input is not a valid number, a ValueError
will be raised, and we can use the except
block to handle this error and notify the user that their input is not valid.
Step 4: Combine the steps into a function
It is often helpful to define a function that handles the entire process of accepting only numbers as input. We can create a function called get_number()
, which will use the input()
function, handle exceptions, and return the valid numerical input to the caller.
Here is the complete function to accept only integers:
1 2 3 4 5 6 7 8 |
def get_integer(prompt="Enter an integer: "): while True: user_input = input(prompt) try: number = int(user_input) return number except ValueError: print("Please enter a valid integer.") |
And here is the same function, modified to accept both integers and decimal numbers:
1 2 3 4 5 6 7 8 |
def get_number(prompt="Enter a number: "): while True: user_input = input(prompt) try: number = float(user_input) return number except ValueError: print("Please enter a valid number.") |
Try to run the code:
1 |
get_integer() |
Output
Enter an integer: test Please enter a valid integer. Enter an integer: 4.5 Please enter a valid integer. Enter an integer: 7
This function also returns true if you use the float value:
1 |
get_number() |
Output
Enter a number: test Please enter a valid number. Enter a number: 5.6
Conclusion
In this tutorial, we have demonstrated how to accept only numerical input in Python.
By using built-in functions such as input()
, int()
, and float()
, as well as error-handling techniques with try
and except
statements, we can effectively ensure that the user provides valid numerical input.
Additionally, we have provided example code for creating functions that handle the process of accepting integers and decimal numbers.