Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Python Classes in 2 Files [Learn Python the Hard Way]
  2. from sys import exit
  3. from random import randint
  4. from ex43princess import PrincessRoom
  5.  
  6. class Game(object):
  7.  
  8.     def __init__(self, start):
  9.         self.quips = [
  10.             "You died. You suck.",
  11.             "Hey, you died. Look at that.",
  12.             "You lose. I win. End.",
  13.         ]
  14.         self.start = start
  15.  
  16.     def play(self):
  17.         next = self.start
  18.  
  19.         while True:
  20.             print "n--------"
  21.             room = getattr(self, next)
  22.             next = room()
  23.  
  24.     def death(self):
  25.         print self.quips[randint(0, len(self.quips)-1)]
  26.         exit(1)
  27.  
  28. a_game = Game("princess")
  29. a_game.play()
  30.        
  31. class PrincessRoom(object):
  32.  
  33.     def __init__(self):
  34.         pass
  35.  
  36.     def princess(self):
  37.         print "text here"
  38.  
  39.         raw_input("> ")
  40.  
  41.         if raw_input == 1:
  42.             return 'eat_it'
  43.         else:
  44.             return 'death'
  45.  
  46.     def eat_it(self):
  47.         print "text here"
  48.        
  49. Traceback (most recent call last):
  50.   File "ex43-2.py", line 29, in <module>
  51.     a_game.play()
  52.   File "ex43-2.py", line 21, in play
  53.     room = getattr(self, next)
  54. AttributeError: 'Game' object has no attribute 'princess'``
  55.        
  56. princessroom = PrincessRoom()
  57. agame = Game(princessroom)