w3resource

Python: Input a number, if it is not a number generates an error message

Python Basic: Exercise-113 with Solution

Write a Python program that inputs a number and generates an error message if it is not a number.

Sample Solution-1:

Python Code:

# Create an infinite loop using "while True."
while True:
    try:
        # Try to read an integer input from the user and store it in variable "a."
        a = int(input("Input a number: "))
        # If successful, break out of the loop and continue with the next code.
        break
    except ValueError:
        # If the input is not a valid integer, an exception (ValueError) is raised.
        # In that case, print an error message and prompt the user to try again.
        print("\nThis is not a number. Try again...")
        print()

Sample Output:

Input a  number: abc                                                                                          
                                                                                                              
This is not a number.  Try again...                                                                           
                                                                                                              
Input a  number: 150 

Sample Solution-2:

Python Code:

# Define a variable "x" and initialize it with the float value 1.23.
x = 1.23

# Check if "x" is an integer by using the "is_integer()" method, which returns a boolean value.
x_int = x.is_integer()

# Print a message to indicate that we are checking if "x" is an integer.
print("Check if x is an integer!")

# Print the result of the check for "x" being an integer.
print(x_int)

# Define a variable "y" and initialize it with the float value 1.0.
y = 1.0

# Check if "y" is an integer by using the "is_integer()" method, which returns a boolean value.
y_int = y.is_integer()

# Print a message to indicate that we are checking if "y" is an integer.
print("Check if y is an integer!")

# Print the result of the check for "y" being an integer.
print(y_int)

Sample Output:

Check if x is an integer!
False
Check if y is an integer!
True

Sample Solution-3:

Python Code:

# Define a variable "x" and initialize it with the float value 1.0.
x = 1.0

# Check if "x" is an integer by using the "isinstance()" function, which returns a boolean value.
x_int = isinstance(x, int)

# Print a message to indicate that we are checking if "x" is an integer.
print("Check if x is an integer!")

# Print the result of the check for "x" being an integer.
print(x_int)

# Define a variable "y" and initialize it with the integer value 1.
y = 1

# Check if "y" is an integer by using the "isinstance()" function, which returns a boolean value.
y_int = isinstance(y, int)

# Print a message to indicate that we are checking if "y" is an integer.
print("Check if y is an integer!")

# Print the result of the check for "y" being an integer.
print(y_int)

Sample Output:

Check if x is an integer!
False
Check if y is an integer!
True

Python Code Editor:

 

Previous: Write a Python program to remove the first item from a specified list.
Next: Write a Python program to filter the positive numbers from a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.