0

I tried this code:

name = str(input("What is your name? "))
if not name.isalpha():
        print("Please enter a valid name.")
        continue

but when I enter a name with a space or full name, I get the "Please enter a valid name." statement even if I only entered a name without any other types of data i.e. int, float.

Does the space count as a string? If not, how should I change my code so that it will accept a full name with spaces but not accept numbers?

Thanks.

BjCuizon
  • 11
  • 1
  • 2

1 Answers1

0

It looks like you just want to check if each character is either a space or a letter. You can do that manually by going through each letter.

name = str(input("What is your name? "))
if not all(x.isalpha or x == "" for x in  name):
    print("Please enter a valid name.")
else:
    print(name)
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • I do not only want to check if each character is a space or letter. I want to check it if it has no number but a valid name, possibly containing space as I will use the data later in a program. – BjCuizon Apr 01 '17 at 02:30
  • @BjCuizon. You need to specify exactly what you mean by a valid name. Without that information, your question is not answerable. – merlin2011 Apr 01 '17 at 02:37