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

Python 如何将已定义函数添加到另一个已定义函数中?,python,function,text-based,Python,Function,Text Based,因此,我是一个创建文本基础游戏的初学者,我所处的情况是,玩家知道如何打开锁着的门,并且能够退出。但我不想让他们在开门前就离开。因此,我想我应该创建一个单独定义的函数,我这样写: def exit_(): if decision == "exit": print("(exit room)") print("You exit the room.") room_2() #level up here elif decisio

因此,我是一个创建文本基础游戏的初学者,我所处的情况是,玩家知道如何打开锁着的门,并且能够退出。但我不想让他们在开门前就离开。因此,我想我应该创建一个单独定义的函数,我这样写:

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_()
然后像这样把它加在一起:

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

在玩家正确输入答案后,解锁门。但它似乎不起作用,有没有办法将两个定义的函数添加到一起,或者我必须使用另一种方法?

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

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()来输出决策

def room_1():
   #do stuff...
   return decision
然后你打电话

exit(room_1())

这不是Python<代码>车门锁定==真??Python是一种动态语言,变量不是类型化的,它是
True
而不是
True
,还有什么是
user.input
???@juanpa.arrivillaga代码是通用的,可以很容易地翻译,例如
correct\u answer=“unlock”
input=input(“”)
if(input==correct\u answer)
等。