0

In my class, we're making a text adventure, and I'm just trying to walk between rooms. So far, I made a class called "Room".

class Room(object):
    def __init__(self,name):
        self.name = name

As well as some rooms to move between with subclasses.

class Backyard(Room):
global current

def __init__(self):
    self.name = "Backyard"

def choose(self,choice):
    if "north" in choice:
        current = kitchen
    else:
        print("You cannot go that way")
    return current

class Kitchen(Room):
    global current

    def __init__(self):
        self.name = "Kitchen"

    def choose(self,choice):
        if "south" in choice:
            current = yard
        else:
            print("You cannot go that way")
        return current

yard = Backyard()
kitchen = Kitchen()

Now, to run the actual game, I have this.

run = True
current = yard
show_yard

while run:
    choose(input(">>> ")
    show_current

The show_yard function just prints a description of that room (the yard), but when I run the game, I was hoping to be able to use show_current so that whatever the variable 'current' was set to, it would read off that description, but instead it gives me an error because there's no function called 'show_current'.

I'm wondering if there is some way to insert variables when calling a function so that I don't have to write a whole bunch of code for something that could be solved in something as simple as this. Thanks.

5
  • please indent your code, and furthermore, global is a serious antipattern. Only in very rare occasions you should use that. Commented Oct 27, 2017 at 8:19
  • 1
    Give each of your locations a show() method. Then all you need to use is current.show() to call the corresponding method on the current location. You can put the method on the base class (and just use self.name to print the name of the location), or give each subclass a more elaborate implementation. Commented Oct 27, 2017 at 8:19
  • You don't need to make current a global variable everywhere. Your choose() methods return the new location, so use current = current.choose() to assign returned location back to current. Not that setting global current on the class even works, you can remove the global current lines with no change in functionality for your current code. Commented Oct 27, 2017 at 8:20
  • Thanks for the responses. First, I don't how I didn't properly indent my code, but this is the first question I've posted here. Second, thanks for the info about the global variable. Finally, I can't believe I didn't think about using a show() method. I just started using classes, and we haven't even learned them in class yet. So thanks, I'll try to figure that out. Commented Oct 27, 2017 at 8:36
  • Alright, update, that worked great, and I cannot express my gratitude enough to all of you guys. Thanks again. Commented Oct 27, 2017 at 8:40

1 Answer 1

1

if you add a show() method to Room, you will be able to call it on every instance of its subclasses:

class Room(object):
    def __init__(self,name):
        self.name = name
    def show(self):
        print(self.name)
Sign up to request clarification or add additional context in comments.

Comments

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.