Variables in nested functions

python

#1

This is my doubt:
I’m trying to write a text-adventure game with nested statements and functions.
And the problem is that I don’t know how to declare a function that will then go in a nested function. An example will be easier.

  1. I’m a character and I have to choose where to go. (1st function)
  2. I choose to go to open a door. (2nd function)
  3. I try to open it, but it is closed, so I call a function that will take me back to the point where I have to choose what to do. (to first function)
  4. I would like that if I choose to go again to open the door, the program will say “Hey, you already tried to do that” and then takes me back to the point to choose where to go.

So how can I do to insert a variable, that will make the program behave differently in the same function?

I hope I was clear enough. If not I will try to write some code directly here. Thank you


#2

It’d definitely be best if you tried and showed us some code first?


#3

perfect! That’s what I was trying to do:

def door():
    open_door =  input("Are you going to open the door?\n> ")
    if open_door == "yes":
        print("There is no way that door is going to open")
        door_closed = "x"
        explore_room()

def explore_room():
     
    choice = input("You have 3 options:\n1. open the door\n2. go to the TV.\n3. go to the mirror.\n> ")
    if choice == "door":
        print("You go to the door")
        door()
    elif choice == "door" and door_closed == "x":
        print("Why should you go there again?")
        explore_room()

explore_room()

I didn’t insert the other pieces of code concerning the other parts of the room.
I just would like to know how I can insert a variable that meets my condition so that in that elif statement I wouldn’t just repeat only the entire function, but add something more, like that little sentence. I hope now it is clearer


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.