[英]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.