mercredi 1 août 2018

Undestanding A While Loop With Conditionals

I am just beginning with python and need some help understanding the logic. The micro programm am writing will ask the user to enter name and verify if the name has spaces, returning error and asking the user to reenter. ( I know I can use the isalpha() function to make it happen), but I want to know what I am doing wrong here, that the program runs for first time and the after I re-enter the name even with spaces, the execution will happen. Thanks in advance

s = input("Please enter your name: ")
def has_space(item):
    for i in item:
        if i.isspace():
            print('Cannot contain spaces.')
            s = input("Please enter your name: ")
while 1:
    if has_space(s):
        print('0')
    else:
        break


print('Welcome ' + s)

Aucun commentaire:

Enregistrer un commentaire