-3

How do i validate a user input in python ? I am writing this code , the user has to put in their name, and it has to be two names separated by a space and the name should contain only letters

def Name():
    n = 0
    name = input('name please\n>>')
    c = name.split(' ')
    while len(c) != 2:
        print('we need two names')
        name = input('name please\n>>')
        c = name.split(' ')

    while True:
        for i in name:
            if i not in r:
                n+=1
        if n > 0:
            print('Letters only')
        else:
            break

Name()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • [isalpha python function won't consider spaces](//stackoverflow.com/a/20890671) – 001 Jan 10 '20 at 23:54
  • What is the issue with your current code? – AMC Jan 11 '20 at 00:00
  • i want the programme to return a two names with only letters but i find it hard to validate the input. when i use the while loop and the for loop, it runs twice and stops – christian zelioh Jan 11 '20 at 00:29
  • 2
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) Use this in conjunction with the validation method in the answer below. – iz_ Jan 11 '20 at 00:31
  • ' def Name(): name = input('name please\n>>') c = name.split(' ') if len(c) != 2: print('we need two names') Name() for item in c: if not item.isalpha(): print('Letters only') Name() return name Name()' – christian zelioh Jan 11 '20 at 00:31

2 Answers2

0

Using while loop

def Name():
  while True:
    name = input('name please:')
    c = name.split(' ')
    if len(c) != 2:
      print("We need two names (first/last with space between)")
      continue

    if not (c[0].isalpha() and c[1].isalpha()):
      print('Only letters a-z or A-Z') 
      continue

    return name

name = Name()
print(name)
DarrylG
  • 16,732
  • 2
  • 17
  • 23
-1

Use .isalpha() and I suggest just doing this recursively.

def Name():
    name = input('name please\n>>')
    c = name.split(' ')
    if len(c) != 2:
        print('we need two names')
        Name()

    for item in c:
        if not item.isalpha():
            print('Letters only')
            Name()

    return name

Name()
Randy Maldonado
  • 352
  • 1
  • 7
  • thank you very much. Is there another way rather than calling the function within the function?. like using a while loop? – christian zelioh Jan 11 '20 at 00:27
  • Doing this recursively is a terrible idea, and why haven’t you commented on the problematic globals-based design? – AMC Jan 11 '20 at 01:04
  • AMC, as I take a second look, I see recursion can get messy and a while loop would be better. I don't know what you mean globals-based design, however if you see a problem with the code you should make a comment on the post so that the poster and other readers realize said problem and not ask others to do it. – Randy Maldonado Jan 11 '20 at 18:29