2012-12-04 5 views
2

발행 하나의 커다란 문서 문자열 인 다른 함수를 출력하십시오. 여기 파이썬 문서화 문자열 프린터는 내가 데이빗 쇼의 LPTHW을 통해 내 방식에 전원을 공급하고있어 내가에, 함수를 만들려고 노력하고 있어요 그래서 여기에 3</p> <p>(신규) 운동 43 여분의 신용에 쓰러진 나무에 왔어요

엔진

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

왜 문서 문자열을 들여 씁니까? 함수 몸체에 들여 쓰거나 들여 쓰지 않은 선들이 있기 때문에 정말보기 흉한 것처럼 보입니다. 문서 문자열의 텍스트를 지정하려면 [documentation] (http://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation) – Bakuriu

+0

의 예제를 따라 할 수 있습니다. 전체 멍청한 놈과 나는 콘솔에서 좋은 모습을 얻는 데 약간의 어려움을 겪고 있습니다. 약간의 사례가 있습니다. – 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