Abdullah Bilal Abdullah Bilal - 3 months ago 7
Python Question

Is there some way to make sure that the raw_input is only non-numeric?

I want to make sure that the user enters the input in the correct format and i do know how to make sure its numeric input but i dont understand how to make sure its not numeric input.

x = raw_input('What is your name? ')
y = raw_input('What is your age? ')
try:
something_to_make_sure_its_not_numeric(x)
int(y)
print "Hello {0}, You look very handsome for someone who is {1}.".format(x, y)
except:
print('please enter input in correct format')


y question is that is there some function in python like int() that makes sure that it only has letters.

Answer

There is also isdigit() function with string to check whether string is numeric or not. To check whether it is non-numeric, simply use not with the condition

>>> 'xyx'.isdigit()
False
>>> '123'.isdigit()
True

# Now check output with "not"
>>> not 'xyx'.isdigit()
True
>>> not '123'.isdigit()
False

It will be good idea to create your custom function to achieve this using isdigit()

def is_non_numeric(num_str):
    return not num_str.isdigit()

# Example
>>> is_non_numeric('123')
False
>>> is_non_numeric('xyz')
True