简体   繁体   English

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

[英]How do I add a defined function into another defined function?

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?但它似乎不起作用,有没有办法将两个定义的函数添加在一起,或者我可能必须使用另一种方法?

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
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然后你让 room_1() 输出决定

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

Then you call然后你打电话

exit(room_1())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我在另一个 function 中定义的值调用到另一个 function? - How do I call the values that I have defined in another function to the other function? 如何调用 function 打印在另一个 function 中定义的字符串变量? - How do I call a function that prints a string variable which is defined in another function? 如何在另一个 function 中使用来自一个 function 的本地定义变量? - How do I use a locally defined variable from one function in another function? Python - 如何将定义的变量从一个 function 获取到另一个 function? - Python - How do I get the defined variable from one function to another function? 如何调用另一个函数中定义的函数? - How can I call a function defined in another function? 当我在另一个函数上定义它时,为什么我得到名称未定义的错误? - Why do i get Name is not defined error for python when i defined it on another function? 如何使用在函数内部,函数外部定义的变量? - How do i use a variable that is defined inside of a function, outside of a function? 调用另一个函数中定义的函数 - Call a function defined in another function 如何将raw_input()与用户定义的函数一起使用? - How do I use raw_input() with a user defined function? 如何将变量从已定义的函数传递到while循环? - How do I pass a variable from a defined function to a while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM