
Untitled
By: a guest on
Jun 30th, 2012 | syntax:
None | size: 1.22 KB | hits: 11 | expires: Never
Python Classes in 2 Files [Learn Python the Hard Way]
from sys import exit
from random import randint
from ex43princess import PrincessRoom
class Game(object):
def __init__(self, start):
self.quips = [
"You died. You suck.",
"Hey, you died. Look at that.",
"You lose. I win. End.",
]
self.start = start
def play(self):
next = self.start
while True:
print "n--------"
room = getattr(self, next)
next = room()
def death(self):
print self.quips[randint(0, len(self.quips)-1)]
exit(1)
a_game = Game("princess")
a_game.play()
class PrincessRoom(object):
def __init__(self):
pass
def princess(self):
print "text here"
raw_input("> ")
if raw_input == 1:
return 'eat_it'
else:
return 'death'
def eat_it(self):
print "text here"
Traceback (most recent call last):
File "ex43-2.py", line 29, in <module>
a_game.play()
File "ex43-2.py", line 21, in play
room = getattr(self, next)
AttributeError: 'Game' object has no attribute 'princess'``
princessroom = PrincessRoom()
agame = Game(princessroom)