GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Input a number, if it is not a number generates an error message - w3resource

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

Last update on May 17 2025 05:44:18 (UTC/GMT +8 hours)


Number Input Validator

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

For more Practice: Solve these Related Problems:

  • Write a Python program to check if user input is an integer or a floating-point number.
  • Write a Python program to validate user input to ensure it is a positive number.
  • Write a Python program to check if a given input can be safely converted to an integer.
  • Write a Python program to prompt the user for input until a valid number is entered.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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.

Python Code Editor:

 

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.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.