Why is my if loop returning 'None'?

I’m totally new to coding and am working on a function that gets a name from a potential player of the game. This code works if the player answers “T” on the fourth line

istrue = input(f"\nT or F: {first_name} {last_name} is your name? ")

But if the player enters “False”, when the loop repeats itself, it returns ‘None’ in the second function. I don’t know why. Also, I don’t know if I am formatting this question properly in the forum.

Thanks for any input!

def getname():
        first_name = input("What is your first name? ")
        last_name = input("What is your last name? ")
        istrue = input(f"\nT or F: {first_name} {last_name} is your name? ")
        if istrue.lower() == 't' or istrue.lower() == 'true':
            name = first_name + ' ' + last_name[0]
            return name
            print(f"\nThank you, {name}!\n")
        elif istrue.lower() == 'f' or istrue.lower() == 'false':
            print("\nMy mistake...")
            getname()
        else:
            print("\nI'm sorry, that was not an option.\n")
            getname()


    def quest():
        print(f"{player}, do you want to go on a quest? ")
        questing = input("1. Yes \n2. No\n   > ")
        if questing == '1':
            print(f"Too bad, {player}.")
        elif questing == '2':
            print(f"Too bad, {player}...there's no returning now! Muah ha ha!")
        else:
            print(f"""{player}, that wasn't an option.
            \nLet's try this again...\n""")
            quest()


    player = getname()
    playing = quest()

Pretty easy, on one line you do this:

return name

So inside any call to getname that reaches this line your function will end and return name. However, why do you then have lines like this:

        else:
            print("\nI'm sorry, that was not an option.\n")
            getname()

In that last line you call getname, but you do not return what it returns. Let’s say you had a sequence of steps that did this:

  1. Call get name from the top level script. This is call #1.
  2. Inside that call (#1) it calls getname() again, call this call #2.
  3. Inside call #2 you return the name “Frank”.
  4. Call #2 exits back to call #1. You are now back in the first call #1 on that last line.
  5. That means your #2 call returned something, but when it got back to the #1 call that value “Frank” just died and your function exits.

The solution is to simply do this:

return getname()
1 Like