0

Apologies for the vague title. I wasn't sure how to make it specific enough.

I have some code that asks for a word and then turns it into a list of characters. I wanted to pass it through a loop to check in its isalpha().

When I run it. If my word is alpha it works fine. If my word has digits, it will restart the loop as planned and ask again.

However, the word gathered after a digit attempt will return a "None" and throw up an error when it tries to turn it into a list.

def start():
    word = input("Enter word to turn into a list")
    if word.isalpha():
        word = word.lower()
        return word
    else:
        print("Alpha only please")
        start()

user = start()
word_list = list(user)
print (word_list)
Pigeon
  • 11
  • 1
  • 1
    Because you didn’t `return` anything in or after the else. None is the default – Mark Tolonen Oct 21 '22 at 17:47
  • Because there's no `return` in the `else` clause. `None` is implicitly the default return value. – Random Davis Oct 21 '22 at 17:47
  • (1) You shouldn't use a recursive call here but wrap it in a "while True:" loop. (2) If you really want a recursive call, you must return the value again that is returned by the recursive call. – Michael Butscher Oct 21 '22 at 17:49
  • 1
    Also because you shouldn’t call `start()` to go back to the top. Use a while loop. Even when you get a correct value the second time, the second start doesn’t return anything. Use a while loop. – Mark Tolonen Oct 21 '22 at 17:50
  • Or if you really insist on using recursion (which you shouldn't. I mean, I come from functional programming, and I love recursion. I was taught to use recursion instead of for, while, and practically any kind of control structure. But then, it was using scheme. Python is just not the language for using recursions instead of while), so, I you really want to use recursion anyway, then you should replace ``start()`` by ``return start()``. Then, the 1st call will return what the 2nd call returns, and the second call returns the word (if the 2nd attempt is the good one) – chrslg Oct 21 '22 at 19:01

1 Answers1

0

This is because an implicit return None is observed in cases where a function does not return a value.

The above code is the same as below:

def start():
    word = input("Enter word to turn into a list")
    if word.isalpha():
        word = word.lower()
        return word
    # all paths above return a value, so technically `else` is redundant here
    # else:
    print("Alpha only please")
    start()
    ## added ##
    return None

However, as mentioned in the comments, what you really want to use here is a while loop:

def start():
    while 1:  # infinite loop, exited with a `return` or `break` statement
        word = input("Enter word to turn into a list: ")

        if word.isalpha():
            # looks good, exit loop and return the value
            return word.lower()

        print("Alpha only please\n")
        # continue to the next `while` loop iteration

user = start()
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53