def login(log = 1):
    
    con = input(f"{na} are you Ready for the game Yes/NO ")
    if ((con == "Yes")or(con == "yes")or(con == "y")or(con == "Y")):
        print("\nwelcome to The Game") # first time it will return true but recursive function call itself then it will return none 
        return True

    elif(log == 3):
        print("retry after long time")
    else:
        print("Retry again")
        log = log + 1
        login(log)

flag = login()
print(flag)

expected output : flag = True

output : flag = None

2

There are 2 best solutions below

0
BitsAreNumbersToo On

So close, you are only missing a return statement on the recursive call, so that the return value from the time it succeeds makes it back out to the original call, I have show that here.

def login(log=1):
    con = input(f"{na} are you Ready for the game Yes/NO ")
    if ((con == "Yes") or (con == "yes") or (con == "y") or (con == "Y")):
        print("\nwelcome to The Game")  
        return True

    elif (log == 3):
        print("retry after long time")
    else:
        print("Retry again")
        log = log + 1
        return login(log) # this line required the return

na = 'YOURNAME'
flag = login()
print(flag)

Let me know if you have any questions.

As pointed out in comments by @stephan, there is another question with some more explanation here: Why does my recursive function return None?

0
crazyhazy On

@BitsAreNumbersToo, you are correct, but you missed another return when the last retry was triggered.

The last condition (when log = 3) is only printing and returning None, which is why you are receiving a None output.

def login(log=1):
    con = input(f"{na} are you Ready for the game Yes/NO ")
    if ((con == "Yes") or (con == "yes") or (con == "y") or (con == "Y")):
        print("\nwelcome to The Game")  
        return True

    elif (log == 3):
        print("retry after long time")
        return True #Or whatever return value you need
    else:
        print("Retry again")
        log = log + 1
        return login(log) # this line required the return

na = 'YOURNAME'
flag = login()
print(flag)

Let me know if you have any questions