2012-12-04 39 views
2

我通過供電肖捷思銳的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() 

調用,但我得到的是

-------- 
> 

編輯:爲了順利完成練習,我需要編輯引擎打印出文檔,而不是編輯該功能。

+0

爲什麼不縮進文檔字符串?我認爲它看起來很醜,因爲你在函數體中有縮進和非縮進的行。 如果您想縮進文檔字符串的文本,您可以按照[文檔](http://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation) – Bakuriu

+0

中的示例進行操作,米總noob,我有一些麻煩,讓它看起來不錯,在控制檯,虐待研究該示例有點 – lerugray

回答

1

你現在擁有它的方式,current_room永遠不會改變,所以它總是會打印任何初始值爲current_room的文檔字符串,如果有的話。

看來你根本不需要current_room。你可以刪除行room = current_room,只是寫:

print room.__doc__ 

編輯:我看着actual exercise,顯然有更多的central_corridor比你在你的問題已經表明 - 它要求用戶輸入。由於您希望在用戶輸入之前顯示文檔字符串,因此您應該在之前打印文檔字符串,然後調用room()

+0

仍然不打印任何東西我的一端 – lerugray

+0

但感謝注意:) – lerugray

+0

試圖最後一個,我仍然得到一個空白的信息,讓我很難理解。 – lerugray