1

I have a program that asks the user's name:

while True:
try:
    name = str(input("Please enter your name > "))
except ValueError:
    print("Please enter a valid name")
    continue
else:
    break

I want to prevent the user from entering an integer, but with the code above integers are accepted in a string. How can I prevent the user from entering an integer in the string?

2
  • Are you checking if the string is just numbers? Commented Mar 25, 2015 at 20:29
  • just text, no numbers Commented Mar 25, 2015 at 20:31

4 Answers 4

6

Firstly, do not cast str as input returns an str. Note from the docs

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that

After you get the input into name you can have a if condition.

name = str(input("Please enter your name > "))
if (re.search('\d',name)):
     print("Sorry your name contains a number")

And don't forget to import re

Sign up to request clarification or add additional context in comments.

Comments

1

break when trying to cast to an int if an exception is raised as it is not an int:

while True:
    name = input("Please enter your name > ")
    try:
        int(name)
    except ValueError:
        break        
    print("Please enter a valid name")

str.digit might work also but will fail on negative input.

To check if any character is a digit use any:

while True:
    name = input("Please enter your name > ")
    if any(ch.isdigit() for ch in name):
        print("Please enter a valid name")
    else:
        break

You could also create a set of accepted characters:

from string import ascii_letters
st = set(ascii_letters)
while True:
    name = input("Please enter your name > ")
    if not st.issuperset(name):
        print("Please enter a valid name")
    else:
        break

Where you might want to add -, " " and any other potential characters.

3 Comments

Oops, Forgot that you could cheat try
You could also use str.isalpha.
@MalikBrahimi, foo-bar is a valid name as is Lord foobar
0

You can use the string method isdigit() to check if the string is just integers.

name = input("Please enter your name: ")
if name.isdigit() == True:
    print ("That's not a name!")

Likewise, you can also use the method isalpha() to check if the string is just text. However, if there's a space, it will return False.

name = input("Enter your name: ")
if name.isalpha() != True:
    print ("That's not a name!")

4 Comments

if name.isdigit() will do but -100 is a digit and if not name.isalpha() will also be sufficient
The only problem with isalpha() is that some names contain punctuation characters, like a dash.
names also contain spaces
@TigerhawkT3 That's true.
0

Maybe:

if len(set(name) - set('1234567890')) < len(set(name)):
    name = input("Please enter a valid name: ")

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.