8

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
  • Using the raw_input() (Python 2) or input() (Python 3) functions, the return is always a string, never any other type. Commented Feb 7, 2015 at 2:51
  • Can you post the code you have so far ? also, what exactly do you mean by "anything other than string" ? Commented Feb 7, 2015 at 3:08

2 Answers 2

7
  1. Get input from user by raw_input() (Python 2) or input() (Python 3).
  2. Type of variable name is string, so we have to use string method to valid user enter string.
  3. 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
Sign up to request clarification or add additional context in comments.

7 Comments

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
yes, use isdigit() string method to check integer value or use try except with type conversion.
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")
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 .
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")
|
1

You can write an if statement like this:

if not entry.isalpha():
   print ("Invalid entry!")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.