How to use the variable of a function in another function?

Question:

I’m a pre-pre-starter, but I’ve been working on a small game that’s in my mind. It’s a basic ‘Guess Who?’ game that lists some specific people and asks players to pick and keep in mind one of them, then it’ll output this chosen one by asking simple ‘yes or no’ questions. However, I’m having a problem using the variables of several functions in a specific final function.

The function "result_func()" isn’t working and isn’t printing the results of these conditions. And I think that’s because it’s not getting and using the inputs from the previous functions.

Could you please help me with this issue? How can I use get and use these inputs to print results here?

Thank you very much for reading at least, thanks for your time!

You can see the small part of the project below;

yes = "YES, Yes, yes, Y, y, YEAP, yeap, YEAH, yeah"

no = "NO, No, no, N, y, NOPE, nope, NAH, nah"

def game_func():

    g = "x"
    f = "x"
    ...

    def gender_func():
        g = str(input("Am I a woman?t"))
        if g in yes or g in no:
            reality_func()
        else:
            print("Please enter a valid answer!")
            gender_func()

    def reality_func():
        f = str(input("Am I a fictional character?t"))
        if f in yes or f in no:
            alive_func()
        else:
            print("Please enter a valid answer!")
            reality_func()
    ....

    def result_func():
        
        if g in yes and f in no and a in yes and p in yes and h in yes and e in yes:
            print("So, I am 'Angela Merkel'! Right?")
        ...other conditions using AND...
        else:
            print("STOP!") ###Just a temporary ending part###

    gender_func()


game_func()
Asked By: Samet Çolak

||

Answers:

When you initialize g and f in the nested inner functions, new local variables g and f are created that are local to the respective inner functions. To specify that you are referring to the variable of the outer function, qualify the variable names with nonlocal:

def gender_func():
    nonlocal g = str(input("Am I a woman?t"))
    if g in yes or g in no:
        reality_func()
    else:
        print("Please enter a valid answer!")
        gender_func()

def reality_func():
    nonlocal f = str(input("Am I a fictional character?t"))
    if f in yes or f in no:
        alive_func()
    else:
        print("Please enter a valid answer!")
        reality_func()
Answered By: Amal K
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.