-1

I'm just starting to learn classes and objects in Python and I've looked online to find an answer to why it is coming up with the error: "nameError, 'Bedroom' is not defined" and theres been many answers and explanations in that I have to define the class but I just can't see what I'm personally doing wrong its driving me nuts and I know its probably a really stupid mistake, but you learn from them I suppose.

prompt = "> "

class Start():
    print "Project Storm v0.01"
    print "Press Enter to Play"
    raw_input(prompt)
    bedroom = Bedroom(Room)

class Room():
    def enter(self):
        pass

class Bedroom(Room):
    def enter(self):
        print "You wake up dazed and confused with no memory of how you got here."
        print "You find yourself in a dark bedroom with a closed door and a small lamp on the side."
Andy
  • 49,085
  • 60
  • 166
  • 233
KingSwan
  • 3
  • 3
  • 1
    Put `class Start` at the end – inspectorG4dget Oct 24 '14 at 19:25
  • Move your classes into the order Room, Bedroom, Start. The order of definitions matter http://stackoverflow.com/questions/4937532/python-name-resolution-order-of-function-defs – Cory Kramer Oct 24 '14 at 19:27
  • The compiler says it all. You try to use `Bedroom` when it's undefined yet. The interpreter doesn't read ahead. – luk32 Oct 24 '14 at 19:27
  • #inspectorG4dged #Cyber thank you worked a charm but just wondering why that actually works and what difference that makes to how the code is run. – KingSwan Oct 24 '14 at 19:28
  • That is common in many languages, it is (one of) the main reasons that C and C++ use header files. The compiler works top-down, how is it supposed to know that some new class `Bedroom` is coming up if it hasn't reached that point yet? – Cory Kramer Oct 24 '14 at 19:29
  • 1
    maybe learning the basics would be a good step before using classes https://docs.python.org/2/tutorial/classes.html – Padraic Cunningham Oct 24 '14 at 19:32

1 Answers1

0

Try this instead:

class Room():
    def enter(self):
        pass

class Bedroom(Room):
    def enter(self):
        print "You wake up dazed and confused with no memory of how you got here."
        print "You find yourself in a dark bedroom with a closed door and a small lamp on the side."

class Start(): # shouldn't this be Game() or something?
    print "Project Storm v0.01"
    print "Press Enter to Play"
    raw_input(prompt)
    bedroom = Bedroom(Room)
    # Bedroom has now been defined, so it knows what that is.

However it's kind of a strange structure. Give me a minute I can come up with something a lot more extendable....

PROMPT = "> "

class Room():
    def __init__(self, msg):
        self.msg = msg
    def enter(self):
        print msg

class Bedroom(Room):
    pass
    # there's not actually anything special about this now, since all Room objects
    # will have a message. Maybe you have something special here?

class Game():
    def __init__(self):
        print "Project Storm v0.01"
        print "Press Enter to Play"
        raw_input(PROMPT) # all caps for constants
        self.rooms = {}
        self.rooms["bedroom"] = Bedroom("""You wake up dazed and confused with no memory of how you got here.
You find yourself in a dark bedroom with a closed door and a small lamp on the side.""")
    def start(self):
        self.rooms['bedroom'].enter()

if __name__ == "__main__":
    game = Game()
    game.start()

Now you can more easily add on rooms with their own descriptions and pace your game appropriately.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • yes Padraic ive sorted that out since but its more the fact i had to change the order was confusing me. thanks for all the help! – KingSwan Oct 24 '14 at 19:37
  • @KingSwan if you'd put your initialization logic in `__init__` (e.g. inside `Game.__init__` goes: `self.bedroom = Bedroom()`), it wouldn't matter what order it was in! :) – Adam Smith Oct 24 '14 at 19:38
  • oh i see yes that makes sense also thanks for the great answer very helpful and yes the bedroom(Room) will have something special in it eventually :P – KingSwan Oct 24 '14 at 19:42
  • @KingSwan basically it would work that way because the compiler doesn't introspect a function until it's called, and by then `Bedroom` would be defined REGARDLESS OF ORDER. – Adam Smith Oct 24 '14 at 19:43