0

I have this program:

word = input('Customer Name: ')
def validateCustomerName(word):
    while True:
        if all(x.isalpha() or x.isspace() for x in word):
            return True

        else:
            print('invalid name')
            return False

validateCustomerName(word)

I want the program to repeat to ask the user to input their name if their name is entered wrongly such as if it has numbered in it. Return True if the name is valid and False for invalid

output:

Customer Name: joe 123
invalid name

expected output:

Customer Name: joe 123
invalid name
Customer Name: joe han
>>>

Am I missing something in the program?...Thanks

Joe han
  • 171
  • 11
  • Possible duplicate of [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) – SiHa Oct 16 '17 at 07:24

2 Answers2

1

Any return statement from within a function definition will exit the enclosing function, returning the (optional) return value.

With that in mind, you could refactor to something like:

def validateCustomerName(word):
    if all(x.isalpha() or x.isspace() for x in word):
        return True
    else:
        print('invalid name')
        return False

while True:
    word = input('Customer Name: ')
    if validateCustomerName(word):
        break
Andrew Guy
  • 9,310
  • 3
  • 28
  • 40
1

This should serve your purpose:

def validateCustomerName(word):
    while True:
        if all(x.isalpha() or x.isspace() for x in word):
            return True
        else:
            print('invalid name')
            return False

while (True):
   word = input('Customer Name: ')
   status = validateCustomerName(word)
   if status: 
       print ("Status is:",status)
       break
SiHa
  • 7,830
  • 13
  • 34
  • 43
Gadi
  • 21
  • 4