1

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)
Momo
  • 523
  • 3
  • 11
  • 23
  • 1
    Try putting some "return True" You need it because you used that function in if statement. – Seungho Lee Aug 01 '18 at 09:27
  • Close 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) - not exact one but the thread might give you much better ideas how to accomplish what your code wants to do – Patrick Artner Aug 01 '18 at 09:31

6 Answers6

4

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: ")
meissner_
  • 556
  • 6
  • 10
3

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
Luca Cappelletti
  • 2,485
  • 20
  • 35
2

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.

quest
  • 3,576
  • 2
  • 16
  • 26
1

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

Nikhil Rathore
  • 170
  • 2
  • 14
1

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

Jay272600
  • 168
  • 1
  • 8
0

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.

Upasana Mittal
  • 2,480
  • 1
  • 14
  • 19