【问题标题】:Python: How can I return a function as True and apply it to an earlier function to change the true/false statementPython:如何将函数返回为 True 并将其应用于较早的函数以更改真/假语句
【发布时间】:2014-10-02 19:34:49
【问题描述】:

我刚刚开始使用 Python 2 和一般编程,并决定进行一次文本冒险以进行一些练习,但我完全被 title 所困。

这是目前为止的代码。我几乎完全猜测了整个真/假的东西,但我想要解决的是:当你进入 "viewing_room" 时,你会遇到一扇锁着的门,但如果你去 "lab_room" 并得到钥匙卡从身上就可以打开了。

我试图让锁着的门是假的,但是当你收集钥匙卡时,它会变成真,门就会解锁。感谢您的帮助,谢谢!

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

【问题讨论】:

  • lab_room() 在您的退货声明后无法在 search_body 中访问
  • 你应该看看类而不是函数。
  • 您需要能够以某种方式存储状态。通常这是用对象完成的,这确实是何时使用面向对象编程的主要示例。在 Python 中研究 OOP。
  • class ic:在破坏 self 之前检查您的 self ..... oop s
  • 问题是你需要随时保持游戏的状态。使用类很常见,但您也可以创建一个包含键/值对的字典,用于跟踪启用/禁用哪些功能。该 dict 可以是全局变量,或者更好的是,它可以传递给函数。这与持有状态的类非常相似,但更加解耦。

标签: python python-2.7


【解决方案1】:

嗯,在我看来,这不是一个 Python 问题,而是一个通用编程问题。您需要某种方式来存储程序的状态——特别是,之前是否搜索过主体。您应该阅读一些关于state machines 的内容,或者查看在不同函数之间传递有状态对象(可能是保持某种状态的“玩家”或“角色”对象)。

顺便说一句,当与 if 中的布尔值进行比较时,您可以简单地这样做:

if search_body():
    pass

或:

if not search_body():
    pass

而不是与TrueFalse 进行比较。

【讨论】:

  • 现在实际上正在调查它,但你能向我解释一下 pass 正在做什么吗,我对它的描述说,“这个块是空的”,我知道一个块是什么(或者可能只是不知道足够好)但是与布尔值相比,这甚至意味着什么或有什么关系?
  • @KingSwan - 在 Python 中,只要有一个以冒号结尾的语句(如 ifforwhile 等),它就会期望后面跟着一个块。该块必须包含至少一个语句。由于想要定义一个什么都不做的块是很常见的(例如,如果您想要定义一个“空”函数),因此创建了 pass 语句(它什么都不做)。这样,由于一个块必须包含至少一个语句,如果你想要一个什么都不做的块,你可以使用pass 语句来创建一个什么都不做的有效块。
  • @KingSwan - shevron 在他的示例中使用pass 的原因是作为占位符。如果他忽略了它,他的答案将包含无效的 Python 代码(因为if 语句必须始终后跟一个包含至少一个语句的块)。在您的代码中,如果 search_body() 函数返回 True 或 False,您可能会做一些事情,因此您可以在他放置 pass 语句的地方替换您自己的代码(无论它是什么)。
【解决方案2】:

你应该看看类。如果您制作游戏,那么面向对象编程是一种更好的方式。

当你习惯上课时,制作游戏会变得更容易。

这是您的游戏示例,作为类对象

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 类型的对象,并且会记住它的状态。 def init(self) 是一个在您创建该类的新对象时将被调用的方法。你可以根据需要创建任意数量的 Game 对象,名称 newGame 或任何你称之为新对象的名称都将替换 self。所以 player1.searchedBody 可能为真,而 player2.searchedBody 可能为假。这很方便。

【讨论】:

  • 为什么这个效果更好?我假设自己。指的是玩家,它是否保持他们的状态,如果是这样,为什么不使用类就不那么容易编码?你能解释一下 def __init__(self): 函数在做什么吗?
  • 更简单的说法可能是错误的。但是如果你使用类,每个对象都有它的方法,就像玩家一样,dealDamage 和 takeDamage move jump 等。你也可以用字典和函数来做它,就像一个人一样
猜你喜欢
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多