【问题标题】:python docstring printer issuespython docstring打印机问题
【发布时间】:2012-12-04 13:42:54
【问题描述】:

我正在努力通过 Zed Shaw 的 LPTHW,但在(新)练习 43 额外学分 3 中​​遇到了障碍。

所以我在这里尝试创建一个函数,打印另一个函数,它是一个大文档字符串。

这是引擎

class Game(object):

    def __init__(self, start):
        self.quips = [
            "You died.  You kinda suck at this.",
            "Your mom would be proud. If she were smarter.",
            "Such a luser.",
            "I have a small puppy that's better at this."
        ]
        self.start = start

    def play(self):
        # next_room_name is taken from init argument start
        next_room_name = self.start

        while True:
            print "\n--------"
            # set variable room to get function from next_room_name
            room = getattr(self, next_room_name)
            # unpacks function from next_room_name into room
            next_room_name = room()

            room = current_room
            print current_room.__doc__

这是我试图调用的一些函数来启动游戏。

    def central_corridor(self):
        """
The Gothons of Planet Percal #25 have invaded your ship and destroyed
your entire crew.  You are the last surviving member and your last
mission is to get the neutron destruct bomb from the Weapons Armory,
put it in the bridge, and blow the ship up after getting into an 
escape pod. 

You're running down the central corridor to the Weapons Armory when
a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume
flowing around his hate filled body.  He's blocking the door to the
Armory and about to pull a weapon to blast you.
        """

        action = raw_input("> ")

        if action == "shoot!":
            print "Quick on the draw you yank out your blaster and fire it at the Gothon."
            print "His clown costume is flowing and moving around his body, which throws"
            print "off your aim.  Your laser hits his costume but misses him entirely.  This"
            print "completely ruins his brand new costume his mother bought him, which"
            print "makes him fly into an insane rage and blast you repeatedly in the face until"
            print "you are dead.  Then he eats you."
            return 'death'

        elif action == "dodge!":
            print "Like a world class boxer you dodge, weave, slip and slide right"
            print "as the Gothon's blaster cranks a laser past your head."
            print "In the middle of your artful dodge your foot slips and you"
            print "bang your head on the metal wall and pass out."
            print "You wake up shortly after only to die as the Gothon stomps on"
            print "your head and eats you."
            return 'death'

        elif action == "tell a joke":
            print "Lucky for you they made you learn Gothon insults in the academy."
            print "You tell the one Gothon joke you know:"
            print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
            print "The Gothon stops, tries not to laugh, then busts out laughing and can't move."
            print "While he's laughing you run up and shoot him square in the head"
            print "putting him down, then jump through the Weapon Armory door."
            return 'laser_weapon_armory'

        else:
            print "DOES NOT COMPUTE!"
            return 'central_corridor'

这是它在主程序中的调用方式

a_game = Game("central_corridor")
a_game.play()

但我得到的只是

--------
> 

编辑:为了成功完成练习,我需要编辑引擎以打印出文档字符串,而不是编辑函数。

【问题讨论】:

  • 为什么不缩进文档字符串?我认为它看起来真的很丑,因为你在函数体中有缩进和非缩进的行。如果您想减少文档字符串的文本,您可以按照documentation 中的示例进行操作
  • 我是一个完全的菜鸟,我在让它在控制台中看起来不错时遇到了一些麻烦,我研究了一下这个例子

标签: python docstring


【解决方案1】:

你现在拥有它的方式,current_room 永远不会改变,所以它总是会打印 current_room 的初始值的文档字符串,如果有的话。

看来你根本不需要current_room。你可以删除room = current_room这一行,然后写:

print room.__doc__

编辑:我查看了actual exercise,显然central_corridor 中的内容比您在问题中显示的要多 - 它要求用户输入。由于您希望文档字符串在用户输入之前显示,您应该在调用room() 之前打印文档字符串

【讨论】:

  • 仍然没有打印任何内容
  • 试过最后一个,我仍然收到一条空白消息,让我大吃一惊。
  • @lerugray:查看我的编辑,问题中缺少信息。
  • 行得通!太感谢了!我担心我没有发布足够的信息。
  • 只是说,你的引擎很乱,你真的应该为此定义一个更好的方法。如果你真的想要一个好的应用程序,首先定义一个类Room :P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多