-1

So I'm a beginner creating a text-base game, and I am in a situation where the player figures out how to unlock a locked door and are capable of exiting. But I don't want them to exit the door before they unlocked it. Therefore, I though I'd create a separate defined function which I wrote like this:

def exit_():
    if decision == "exit":
        print("(exit room)")
        print("You exit the room.")
        room_2()
        #level up here
    elif decision == "exit prison cell":
        print("(exit room)")
        print("You exit the room.")
        room_2()
        # level up here
    elif decision == "exit the prison cell":
        print("(exit room)")
        print("You exit the room.")
        room_2()

and then added it together like this:

room_1() and exit_()

After the player inputed their answer correctly to unlock the door. But it doesn't seem to work, is there a way to add two defined functions together or perhaps I have to use another method?

toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29
John
  • 37
  • 5

2 Answers2

0

You could use booleans for checking if the user is able to go through the locked door or not:

door_locked == True
if userinput == correct_answer:
    door_locked = False
    # user can do whatever they want now the door is unlocked
    room_2()
else:
    door_locked = True
    # user can do whatever they want but the door is still locked
toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29
Jordan Savage
  • 196
  • 2
  • 16
  • This is not Python... `bool door_locked == true`?? Python is a dynamic language, variables aren't typed, and it is `True` not `true`, also, what is `user.input`??? – juanpa.arrivillaga Oct 14 '17 at 18:28
  • @juanpa.arrivillaga code is generic and can be translated easily for example... `correct_answer = "unlock"` `input = input("")` `if (input == correct_answer)` etc. – Jordan Savage Oct 14 '17 at 18:29
0
def exit_(decision):
    if decision== "exit":
                print ("(exit room)")
                print ("You exit the room.")
                room_2()
                #level up here
    elif decision== "exit prison cell":
                print ("(exit room)")
                print ("You exit the room.")
                room_2()
                #level up here
    elif decision== "exit the prison cell":
                print ("(exit room)")
                print ("You exit the room.")
                room_2()

And you make room_1() to output the decision

def room_1():
   #do stuff...
   return decision

Then you call

exit(room_1())
farbiondriven
  • 2,450
  • 2
  • 15
  • 31