0

Hey guys, I'm currently completing Zed Shaw's "Learn Python the Hard Way" and I've been struggling with Exercise 43, which instructs the learner to make a game. For simplicity, I am attempting to rewrite the previous exercise that has a class Game and some functions:
__init__, play, death, and four more for each "room" in the game.

I was able to copy and modify the code for various conditions, but I wanted to try to split the code in to two files: one file containing a class PrincessRoom to be the sole room for the game, and the other containing the bulk of the old code play and death.

From ex43.py

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()

From ex43princess.py

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"

When I run the code, here's the error I get:

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'``

Now I'm not too solid on why the original code had a_game initialized with a_game = Game("princess") but I'm pretty sure it's directly related to why it has me use room = getattr(self, next). But this is where my understanding falters.

If memory serves, it would appear that Game object isn't inheriting properly from ex43princess.py... right?

If any one could help me understand what is happening here I would be much appreciative.

Thanks! Josh

1
  • where is your game class are you creating a new PrincessRoom object? It's complaining because it's trying to access Princess and it's just a string not an actual object Commented Feb 22, 2011 at 7:56

2 Answers 2

1

Note: I'm not familiar with the book and hence with the context of the question, so my answer refers to the displayed code only

The problem here isn't splitting the code to two files. As far as I can see, the PrincessRoom class in its own file presents no real problem.

Game can't find the princess method, and it doesn't have one. Perhaps it should've inherited from PrincessRoom?

That said, I'm not sure that it makes sense logically to inherit a class named Game from a class named PrincessRoom. A better approach IMHO would be aggregation - i.e. keep a collection of rooms as an instance variable of Game and access them through it. Inheritance should be really reserved to is-a relations. You should ask yourself, whether "Game is-a PrincessRoom" makes sense. It probably doesn't. What does make sense is "Game has-a PrincessRoom", and has-a is represented by aggregation in OOP.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, you're absolutely right. I was looking for a way to learn how to use multiple files but it turns out I've just completely bungled it. And thanks for the 'is-a' vs. 'has-a' differentiation - it explains one of the later chapters. Thanks again!
0

My quick and dirty version of exercise 43. Here. The key is that you are just trying to instantiate the PrincessRoom objects but you can't do it with just a string like you are trying to do.

2 Comments

Here's a slightly more complex one if you think you can handle it :) python.pastebin.com/T06PVjiq
Oh wow, thank you so much for these. I didn't initially understand what was going on with the second one but I'm pretty sure I've got most of it down... Thanks again for something to study!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.