-1
def user_name():
    
    name = input("Enter your name: ")

    if name.lower() == "kitchen crate holster":
        print("Allen please add details")
        
    if not name.strip():
        print("Please enter a name.")
        return user_name()
        
    
    else:
        print("Welcome, ", name + "!")
    

When I run this code I get is this: 'Either all return statements in a function should return an expression, or none of them should.' How can I fix it?

nico9T
  • 2,496
  • 2
  • 26
  • 44
  • 7
    That's not a Python error, it sounds like a lint warning. – Barmar Apr 13 '21 at 20:50
  • 4
    Don't use recursion in place of looping. – Barmar Apr 13 '21 at 20:50
  • 1
    Why do you use `return user_name()`? The function doesn't return anything useful, it just prints things. – Barmar Apr 13 '21 at 20:51
  • 2
    The "error" you're describing doesn't sound like any standard Python exception. Is this possibly a homework assignment, and the "error" is a response from a unit test? Any way, it's saying that there exist multiple branches that flow of execution could take, but only some of them end up returning something from the function explicitly. You need to make sure that both the `if` and `else` blocks return something, or that neither return anything. Since the `return` in the `if` is inappropriate (a loop would be better), I would say don't return anything anywhere. – Paul M. Apr 13 '21 at 20:51
  • You should not think of the error as annoying as it really is passing a message to you: apply the principle that it puts forward, because as it is now your function doesn't look right: shouldn't it return the name that was entered? The error message is actually offering help. Don't think of it as annoying. – trincot Apr 13 '21 at 20:54
  • 1
    Does this answer your question? [How is returning the output of a function different from printing it?](https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it) – c2huc2hu Apr 13 '21 at 20:56
  • A recursive function like this is probably one place where this warning is not appropriate. Ultimately the function is only procedural and does not have an intersting return value – Chris_Rands Apr 13 '21 at 21:00
  • @Chris_Rands mmm no I agree with whatever linter this is... `return user_name()` implies that the return value is useful... but it will always be `None` anyway, so just `user_name()` would be appropriate here. – juanpa.arrivillaga Apr 13 '21 at 21:36
  • This is not an error. This is probably some linter that you are using? Maybe part of your IDE? – juanpa.arrivillaga Apr 13 '21 at 21:36

1 Answers1

0

This isn't a Python error but a pylint warning. It says that your function should always return a value or never return. You are returning your function (?) only in the second if.

If you don't have to return a value but just print, you should rewrite your code like this:

 def user_name():
    
    name = input("Enter your name: ")

    if not name.strip():
        user_name()
    elif name.lower() == "kitchen crate holster":
        print("Allen please add details")
    else:
        print("Welcome, ", name + "!")

This function prints, it doesn't return and it can recall itself (recursion). Instead of recursion I'd use a while loop:

def user_name():
    
    while True:
    
        name = input("Enter your name: ")

        if not name.strip():
            continue
        elif name.lower() == "kitchen crate holster":
            print("Allen please add details")
            break
        else:
            print("Welcome, ", name + "!")
            break 
nico9T
  • 2,496
  • 2
  • 26
  • 44
  • It is probably important to point out, *this isn't a Python error*. – juanpa.arrivillaga Apr 13 '21 at 21:37
  • You're right. I'm adding it to my answer. – nico9T Apr 13 '21 at 21:38
  • I fixed the warning, but now my code does nothing. – pythonman68 Apr 14 '21 at 14:41
  • I just copied my code directly from my answer here and pasted it into a Jupyter cell. I checked all the three cases, it works flawlessly. You should accept my answer because it works and it fixed the "error" you got. If you have other problems you should open another question. – nico9T Apr 14 '21 at 15:44
  • I also added in my answer a version based on a while loop which would be my choice if I had to pick one of the two. I always prefer to avoid recursion if I can. – nico9T Apr 15 '21 at 19:29