Momo
Momo

Reputation: 523

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)

Upvotes: 1

Views: 109

Answers (6)

Jay272600
Jay272600

Reputation: 168

You can achieve your aim with the following piece of code:

s = input("Please enter your name: ")
while ' ' in s:
    print('Cannot contain spaces.')
    s = input("Please enter your name: ")

print('Welcome ' + s)

The statement ' ' in s checks to determine if there are spaces in your string

Upvotes: 1

Luca Cappelletti
Luca Cappelletti

Reputation: 2545

I believe you are trying to achieve the following:

while True:
    s = input("Please enter your name: ")
    if " " not in s:
        break
    print('Cannot contain spaces.')
print('Welcome ' + s)

Let's proceed to breaking down what is wrong with your code, starting from your function:

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

Here, while checking character per character if it is a space, you ask the user to insert a name and you assign it to a local variable s, that does NOT coincide with the global variable s.

This means that you parse the user input, ask for entering a new name for each space in the initially inserted name and do nothing with that.

Additionally, you use this function as a boolean condition in a if, but the function does not return anything: this is treated as returning None and if None is the same as if False.

A better approach could be to separate in two different functions the control and the request for user input, such as:

def has_space(item):
    return " " in item

def ask_name():
    return input("Please enter your name: ")

while 1:
    s = ask_name()
    if has_space(s):
        print('Cannot contain spaces.')
    else:
        break

Upvotes: 3

Nikhil Rathore
Nikhil Rathore

Reputation: 170

You can do this way

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

print('Welcome ' + s)

The problem with your approach is that you are not returning from the function and so the default value that your function is returning is None. So below

while 1:
    if has_space(s):
        print('0')
    else:
        break

else condition is satisfied because value of has_space(s) is None and so it enters the function just once and that's why you are seeing that message of cannot contain space single time. After it comes out of the function it simply breaks. Hope I am clear

Upvotes: 1

Upasana Mittal
Upasana Mittal

Reputation: 2680

As per I could understand what your code does is, 1. Enters while loop 2. Enters has_space function to get some boolean value as if is a conditional and will be implemented only when condition is true otherwise break, That's why it asks for name again only once and then breaks the loop. 3. Prints the 1st input because has_space function is not returning anything so 1st input remains global variable.

Upvotes: 0

meissner_
meissner_

Reputation: 541

The problem here is not with the while condition but has_space because it doesn't return a boolean value which could be evaluated. This causes the if-condition inside your while loop to enter the else branch and exit the while loop.

A possible solution might be a rewrite of the method like:

def has_space(s):
    return ' ' in s

And usage:

while not has_space(s):
    s = input("Please enter your name: ")

Upvotes: 4

quest
quest

Reputation: 3926

Just add a return True and access the global valiable

s = input("Please enter your name: ")
def has_space(item):
    for i in item:
        if i.isspace():
            print('Cannot contain spaces.')
            global s
            s = input("Please enter your name: ")
            return True

while 1:
    if has_space(s):
        print('0')
    else:
        break


print('Welcome ' + s)

The problem is arising because the function is accessing the global variable and it does not return a true or false.

Upvotes: 2

Related Questions