Zyanaster
Zyanaster

Reputation: 29

How to check if input is purely numeric

I'm working on an assignment for my Computer Science class and the assignment asked for us to get an input and determine whether the input is a valid integer and whether the input is a float (2 questions).

I've got most of it down, but the only part that is messing with me is when the input is alphanumeric (i.e. 123dfkj). I tried using

while not num.isdigit():

but a problem comes up when the user input is negative.

Upvotes: 1

Views: 2329

Answers (4)

Two-Bit Alchemist
Two-Bit Alchemist

Reputation: 18467

Be careful with these other answers recommending simply to cast to int, since any floating point value can be truncated to an integer successfully.

You may need to check that the floating point representation of the value is equal to its integer representation, so that, e.g., 3 and 3.0 will count as an integer but not 3.5.

>>> def is_it_a_number(value):
...   try:
...     float(value)
...     print('It can be represented as floating point!')
...   except ValueError:
...     print('It cannot be represented as floating point.')
...   else:
...     try:
...       if float(value) == int(float(value)):
...         print('It is a valid integer.')
...     except ValueError:
...       print('It is not a valid integer.')
... 
>>> is_it_a_number(3)
It can be represented as floating point!
It is a valid integer.

>>> is_it_a_number(-3)                                                      
It can be represented as floating point!                                    
It is a valid integer. 

>>> is_it_a_number(3.0)
It can be represented as floating point!
It is a valid integer.

>>> is_it_a_number(3.5)
It can be represented as floating point!

>>> is_it_a_number('3.0')
It can be represented as floating point!
It is a valid integer.

>>> is_it_a_number('3.5')
It can be represented as floating point!

>>> is_it_a_number('sandwich')
It cannot be represented as floating point.

Upvotes: 0

Tlacenka
Tlacenka

Reputation: 520

Another solution would be using regular expression.

float: ^-?[1-9]{1}[0-9]*.{1}[0-9]*$

integer: ^-?[1-9]{1}[0-9]*$

However, this does not consider using "e" as an exponent (e.g. 6.022e23)

Upvotes: 0

Karl
Karl

Reputation: 3464

Easiest way is to follow the EAFP principle and cast the input to an integer and catch the exception if it isn't.

EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

try:
    myint = int(myinput)
except ValueError:
    # myinput was not an integer

Upvotes: 1

user764357
user764357

Reputation:

This is relatively straight forward by casting the input and using try/except blocks to catch an exceptions.

val = input()

try:
    int(val)
except ValueError:
    print("Not an integer")

try:
    float(val)
except ValueError:
    print("Not a float")

Upvotes: 2

Related Questions