Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何将函数返回为True并将其应用于早期函数以更改True/false语句_Python_Python 2.7 - Fatal编程技术网

Python:如何将函数返回为True并将其应用于早期函数以更改True/false语句

Python:如何将函数返回为True并将其应用于早期函数以更改True/false语句,python,python-2.7,Python,Python 2.7,我刚开始学习Python2和一般的编程,并决定进行一次文本冒险来进行一些练习,但我完全被标题所束缚 这是到目前为止的代码。我几乎完全猜到了所有的真假,但我想弄清楚的是:当你进入“查看室”时,你会遇到一扇锁着的门,但是如果你去“实验室”并从身体上取下钥匙卡,你就可以打开它 我试图使它,使锁着的门是假的,但当你收集钥匙卡,它变为真,门变成解锁。谢谢你的帮助,谢谢 prompt = "> " decision = "What do you do?" not_assigned = "Say wa?

我刚开始学习Python2和一般的编程,并决定进行一次文本冒险来进行一些练习,但我完全被标题所束缚

这是到目前为止的代码。我几乎完全猜到了所有的真假,但我想弄清楚的是:当你进入
“查看室”
时,你会遇到一扇锁着的门,但是如果你去
“实验室”
并从身体上取下钥匙卡,你就可以打开它

我试图使它,使锁着的门是假的,但当你收集钥匙卡,它变为真,门变成解锁。谢谢你的帮助,谢谢

prompt = "> "
decision = "What do you do?"
not_assigned = "Say wa?"
def engine_room():
    print "You are in a dark room with the sound of moaning engines."
    print "You see a corridor to your left and one to your right and an elevator straight ahead of you."
    print decision
    choice = raw_input(prompt)
    if choice == "go left":
        viewing_room()
    elif choice == "go right":
        right_corridor_dead_end()
    elif choice == "use elevator":
        print "you get in the elevator and go up."
        main_hallway()
    else:
        print not_assigned
        engine_room()

def right_corridor_dead_end():
    print "You walk down the corridor only to be blocked by a collapsed ceiling."
    print decision
    choice = raw_input(prompt)      
    if choice == "go back":
        engine_room()           
    else:
        print not_assigned

def viewing_room(): 
    print "You walk down the corridor and enter and a large room with a window covering the size of the wall."
    print "Straight ahead is another door"
    print decision
    choice = raw_input(prompt)
    if search_body() == False:
        if choice == "open door":
            print "The door is locked"
            viewing_room()
        elif choice == "go back":
            engine_room()
        else:
            print not_assigned
    if search_body() == True:
        if choice == "open door":
            print "The door opens you walk through"
            storage_room()
        else:
            print not_assigned
            viewing_room()

def main_hallway():
        print "You enter a large brightly lit room with 3 rooms connected to it and another elevator straight ahead."
        print "The rooms are named, the two on the left are the armoury and lab rooms and to the right are the cabins."
        print decision
        choice = raw_input(prompt)
        if choice == "go to lab room":
            lab_room()
        elif choice == "go back":
            engine_room()

def lab_room():
    print "You enter the lab room which is cluttered with unexplainable machines."
    print "To the back of the room you see the dead body of a man with no obvious cause"
    print "He might have something useful on him"
    print decision
    choice = raw_input(prompt)  
    if choice == "search body":
        search_body()
    elif choice == "go back":
        main_hallway()

def search_body():
    print "You find a keycard that says 'storage' on it."
    return True 
    lab_room()

engine_room()

在我看来,这不是一个Python问题,而是一个一般编程问题。您需要某种方法来存储程序的状态——特别是,无论之前是否搜索过主体。您应该读一点关于或研究在不同函数之间传递有状态对象(可能是维护某种状态的“Player”或“Character”对象)

顺便说一句,与if中的布尔值进行比较时,您只需执行以下操作:

if search_body():
    pass
或:


而不是与
True
False
进行比较

你应该看看课程。如果你做游戏,面向对象编程是一个更好的方法

当你习惯了上课,做游戏就变得更容易了

下面是一个游戏示例,作为类对象

prompt = "> "
decision = "What do you do?"
not_assigned = "Say wa?"

class Game:

    def __init__(self):
        self.bodySearched = False
        self.engine_room()

    def engine_room(self):
        print "You are in a dark room with the sound of moaning engines."
        print "You see a corridor to your left and one to your right and an elevator straight ahead of you."
        print decision
        self.choice = raw_input(prompt)

        if self.choice == "go left":
            self.viewing_room()

        elif self.choice == "go right":
            self.right_corridor_dead_end()

        elif self.choice == "use elevator":
            print "you get in the elevator and go up."
            self.main_hallway()

        else:
            print not_assigned
            self.engine_room()


    def right_corridor_dead_end(self):
        print "You walk down the corridor only to be blocked by a collapsed ceiling."
        print decision
        self.choice = raw_input(prompt)

        if choice == "go back":
            self.engine_room()

        else:
            print not_assigned

    def viewing_room(self): 
        print "You walk down the corridor and enter and a large room with a window covering the size of the wall."
        print "Straight ahead is another door"
        print decision
        self.choice = raw_input(prompt)

        if not self.bodySearched:
            if self.choice == "open door":
                print "The door is locked"
                self.viewing_room()

            elif self.choice == "go back":
                self.engine_room()

            else:
                print not_assigned

        if self.bodySearched:
            if self.choice == "open door":
                print "The door opens you walk through"
                self.storage_room()

            else:
                print not_assigned
                self.viewing_room()


    def main_hallway(self):
            print "You enter a large brightly lit room with 3 rooms connected to it and another elevator straight ahead."
            print "The rooms are named, the two on the left are the armoury and lab rooms and to the right are the cabins."
            print decision
            self.choice = raw_input(prompt)

            if choice == "go to lab room":
                self.lab_room()

            elif choice == "go back":
                self.engine_room()



    def lab_room(self):
        print "You enter the lab room which is cluttered with unexplainable machines."
        print "To the back of the room you see the dead body of a man with no obvious cause"
        print "He might have something useful on him"
        print decision
        self.choice = raw_input(prompt)

        if self.choice == "search body":
            self.bodySearched = True
            print "You find a keycard that says 'storage' on it."
            self.lab_room()

        elif self.choice == "go back":
            self.main_hallway()

newGame = Game()
现在newGame是Game类型的对象,它将记住它的状态。
definit(self)是一个方法,在创建该类的新对象时将调用该方法。你可以创建任意数量的游戏对象,名称newGame或任何你称之为新对象的名称将取代self。所以player1.searchedBody可能为真,而player2.searchedBody可能为假。这非常方便。

lab\u room()
search\u body
中无法访问。在返回语句之后,您应该查看类而不是函数。您需要能够以某种方式存储状态。通常这是通过对象来完成的,这确实是何时使用面向对象编程的一个主要示例。查看Python中的OOP。
class
ic:在破坏
self
之前检查
self
oop
s问题在于,您需要在运行时保持游戏状态。使用类是常见的,但您也可以创建一个dict,该dict具有键/值对,用于跟踪哪些功能被启用/禁用。dict可以是一个全局变量,或者更好,它可以传递给函数。这与保存状态的类非常相似,但更为解耦。为什么这样做效果更好?我假装自己。指的是播放器,它是否保持其状态,如果是,为什么不使用类编写代码就不那么容易?你能解释一下def uu init uuu(self):函数在做什么吗?简单一点可能是错误的说法。但是,如果你使用类,每个对象都有它的方法,就像一个玩家所拥有的,dealDamage和takeDamage-move-jump等。你也可以使用字典和函数来实现它,就像某个meniton现在查看它一样,但你能给我解释一下pass在做什么吗?我对它的描述是“这个块是空的”,我知道什么是块(或者可能还不够好)但这与布尔值相比意味着什么或有什么关系呢?@KingSwan-在Python中,只要有以冒号结尾的语句(比如
if
for
while
,等等),它希望后面有一个块。该块必须至少包含一条语句。由于要定义一个不执行任何操作的块(例如,如果要定义一个“空”函数)非常常见,
pass
语句(不执行任何操作)这样,由于一个块必须至少包含一条语句,如果你想要一个不做任何事情的块,你可以使用
pass
语句来创建一个不做任何事情的有效块。@KingSwan-shevron在他的示例中使用
pass
的原因是作为占位符。如果他不使用它,他的答案将包含在有效的Python代码(因为
if
语句后面必须始终有一个至少包含一条语句的块)。在代码中,如果
search\u body()
函数返回True或False,您可能会执行一些操作,因此您应该将自己的代码(无论是什么)替换到他放置
pass
语句的位置。
prompt = "> "
decision = "What do you do?"
not_assigned = "Say wa?"

class Game:

    def __init__(self):
        self.bodySearched = False
        self.engine_room()

    def engine_room(self):
        print "You are in a dark room with the sound of moaning engines."
        print "You see a corridor to your left and one to your right and an elevator straight ahead of you."
        print decision
        self.choice = raw_input(prompt)

        if self.choice == "go left":
            self.viewing_room()

        elif self.choice == "go right":
            self.right_corridor_dead_end()

        elif self.choice == "use elevator":
            print "you get in the elevator and go up."
            self.main_hallway()

        else:
            print not_assigned
            self.engine_room()


    def right_corridor_dead_end(self):
        print "You walk down the corridor only to be blocked by a collapsed ceiling."
        print decision
        self.choice = raw_input(prompt)

        if choice == "go back":
            self.engine_room()

        else:
            print not_assigned

    def viewing_room(self): 
        print "You walk down the corridor and enter and a large room with a window covering the size of the wall."
        print "Straight ahead is another door"
        print decision
        self.choice = raw_input(prompt)

        if not self.bodySearched:
            if self.choice == "open door":
                print "The door is locked"
                self.viewing_room()

            elif self.choice == "go back":
                self.engine_room()

            else:
                print not_assigned

        if self.bodySearched:
            if self.choice == "open door":
                print "The door opens you walk through"
                self.storage_room()

            else:
                print not_assigned
                self.viewing_room()


    def main_hallway(self):
            print "You enter a large brightly lit room with 3 rooms connected to it and another elevator straight ahead."
            print "The rooms are named, the two on the left are the armoury and lab rooms and to the right are the cabins."
            print decision
            self.choice = raw_input(prompt)

            if choice == "go to lab room":
                self.lab_room()

            elif choice == "go back":
                self.engine_room()



    def lab_room(self):
        print "You enter the lab room which is cluttered with unexplainable machines."
        print "To the back of the room you see the dead body of a man with no obvious cause"
        print "He might have something useful on him"
        print decision
        self.choice = raw_input(prompt)

        if self.choice == "search body":
            self.bodySearched = True
            print "You find a keycard that says 'storage' on it."
            self.lab_room()

        elif self.choice == "go back":
            self.main_hallway()

newGame = Game()