如何将已定义的函数添加到另一个已定义的函数中?

时间:2017-10-14 18:16:29

标签: python function text-based

所以我是一个初学者创建一个基于文本的游戏,我处于这样一种情况:玩家想出如何解锁一扇锁着的门并且能够退出。但我不希望他们在解锁之前退出门。因此,我虽然创建了一个单独定义的函数,我这样编写:

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()

然后像这样将它们加在一起:

room_1() and exit_()

玩家正确地输入答案后解锁门。但它似乎没有用,有没有办法将两个已定义的函数组合在一起,或者我可能需要使用另一种方法?

2 个答案:

答案 0 :(得分:0)

您可以使用布尔值来检查用户是否能够通过锁定的门:

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

答案 1 :(得分: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()

你让room_1()输出决定

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

然后你打电话

exit(room_1())