Being new to programming, I am trying to input name which is a string input. If I enter anything other than a string, error should be displayed as invalid input type. How can this be achieved?
2 Answers
- Get input from user by
raw_input()(Python 2) orinput()(Python 3). Typeof variablenameisstring, so we have to use string method to valid user enter string.- Use
isalpha()string method to check user entered string is valid or not.
code:
name = raw_input("Enter your Last Name:")
if not name.isalpha():
print "Enter only alpha values."
output:
:~/Desktop/stackoverflow$ python 5.py
Enter your Last Name:vivek
:~/Desktop/stackoverflow$ python 5.py
Enter your Last Name:123
Enter only alpha values.
:~/Desktop/stackoverflow$ python 5.py
Enter your Last Name:vivek 123
Enter only alpha values.
Other string methods to check user string is integer or alpha or both
>>> "123".isalnum()
True
>>> "123a".isalnum()
True
>>> "123abc".isalnum()
True
>>> "123abc".isalpha()
False
>>> "123abc".isdigit()
False
>>> "123".isdigit()
True
>>> "123".isalpha()
False
By Type conversion and Exception method
e.g. for invalid input:
>>> a = "123a"
>>> try:
... a = int(a)
... except ValueError:
... print "User string is not number"
...
User string is not number
e.g. for valid input:
>>> a = "123"
>>> try:
... a = int(a)
... except ValueError:
... print "User string is not number"
...
>>> print a
123
>>> type(a)
<type 'int'>
>>>
Ask user to enter value again and again if user enter invalid value.
code:
while 1:
try:
age = int(raw_input("what is your age?: "))
break
except ValueError:
print "Enter only digit."
continue
print "age:", age
output:
vivek@vivek:~/Desktop/stackoverflow$ python 5.py
what is your age?: test
Enter only digit.
what is your age?: 123test
Enter only digit.
what is your age?: 24
age: 24
7 Comments
Srini
Thanks for the quick reply. Where can I get these in build functions?? I am also trying for integers.. is there similar function for integer... if not integer display error
Vivek Sable
yes, use
isdigit() string method to check integer value or use try except with type conversion.Srini
I am getting error AttributeError: 'int' object has no attribute 'isdigit' when I use isdigit() my code is age = int (input ("Enter age:")) if not age.isdigit(): print ("Enter numbers only")
Vivek Sable
yes correct, you are converting user input from
string to int i.e. type casting in statement ` age = int (input ("Enter age:"))` . isdigit() is method of string, not integer. Remove type casting from your code and check. or use exception handling for existing code if user enter any other value then integer .Srini
Thanks for helping. I tried with the try except code, but it is not printing print values. Here is my code... pls check and let me know where I am making mistake in the coding. sorry for bothering I am still learning.. age = int (input ("Enter age:")) try: age = int (age) except ValueError: print ("Entered value is not a number")
|
raw_input()(Python 2) orinput()(Python 3) functions, the return is always a string, never any other type.