﻿ 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

## Python Basic: Exercise-113 with Solution

Write a Python program to input a number, if it is not a number generates an error message.

Sample Solution-1:

Python Code:

``````while True:
try:
a = int(input("Input a number: "))
break
except ValueError:
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
```

## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

``````x = 1.23
x_int = x.is_integer()
print("Check if x is an integer!")
print(x_int)
y= 1.0
y_int = y.is_integer()
print("Check if y is an integer!")
print(y_int)
```
```

Sample Output:

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

## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-3:

Python Code:

``````x = 1.0
x_int = isinstance(x, int)
print("Check if x is an integer!")
print(x_int)
y = 1
y_int = isinstance(y, int)
print("Check if y is an integer!")
print(y_int)
```
```

Sample Output:

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

## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Unpack variables from iterable:

```# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)
```