1

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()
Amal K
  • 3,630
  • 2
  • 16
  • 38
  • 1
    You can use the [global keyword](https://docs.python.org/3/reference/simple_stmts.html#the-global-statement) to assign to a global variable, or better yet, pass the parameters as arguments to the next function – mousetail Jun 24 '21 at 10:31
  • 2
    What do you mean by "isn't working"? Does it throw an error? Have you tried debugging your code? – Maaz Jun 24 '21 at 10:31
  • You introduce yourself unnecessary headache by relying on whether something will be visible in your function or not. A more reliable approach is to pass everything your function needs as *arguments* – Alexey Larionov Jun 24 '21 at 10:31
  • 2
    Another thing you might want to look at is how you have your `yes` and `no` variables as `string` and not a `list`. This, although works, would mean that an input can be `"YES, Yes,"` and it still returns `true`, which you might not want. – Maaz Jun 24 '21 at 10:38
  • By "isn't working", I mean that I only got the output "STOP!" of the last condition 'else'(below) in the last function that finds out the chosen and so that it's not working. " else: print("STOP!") " I edited the yes and no to make them list as below; " yes = ("YES, Yes, yes, Y, y, YEAP, yeap, YEAH, yeah") no = ("NO, No, no, N, y, NOPE, nope, NAH, nah") " – Samet Çolak Jun 24 '21 at 10:54
  • So it is working, just not how you expect it to. I think you've found your solution with the answer below, but I strongly recommend using a debugger, in general, to figure out where exactly is the behavior deviating from what you expect. :) – Maaz Jun 24 '21 at 11:26
  • Firstly, I'm truly sorry if I caused any disturbance to any of you. And thank you all very much! (: So far, I was able to find solutions with the help of this community and also a debugger. But I couldn't understand what the problem is at this time. However, I needed to qualify the variable names with nonlocal; just like Amal K. said so. Thanks again! – Samet Çolak Jun 24 '21 at 11:46

1 Answers1

2

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()
Amal K
  • 3,630
  • 2
  • 16
  • 38
  • I did what you said as "if (nonlocal g in yes or g in no)", but it says "unexpected expression syntax" – Samet Çolak Jun 24 '21 at 10:59
  • @SametÇolak You only need to use non-local once when you are initializing. Don't add it everywhere before using the variable. See my code. Also if you are not assigning to it and directly reading it like in `result_func`, you can skip `nonlocal`. – Amal K Jun 24 '21 at 11:04
  • 1
    Thank you very very very VERY much! Youreka! – Samet Çolak Jun 24 '21 at 11:11
  • @SametÇolak Good to know it helped! If the answer solves your problem, you can accept the answer by clicking on the checkmark on its left :) – Amal K Jun 29 '21 at 15:48