Ex45 'solution' - moving from class to class

I have put the classes in different files - how do I import a class from another file into another class?

Hello @GK77

I think this thread in Stack overflow explain this very well.

I hope this will give you the help you need.

Thanks for that, I think I have that working now, the problem is: can I import a class into an if statement and make it run?

hello @GK77

I tried to make a good example to show this.
The f-string in the print statement requires python3).

First the class that is imported:

# This file:
# fruit.py

class Fruit(object):
    def into_basket(self): # This method..
        return 'apples'

Then the class to use this:

# This file:
# fruit_basket.py

from fruit import Fruit

class Basket(object):

    def fill_basket(self):
        if d.into_basket() == 'apples': # appears here..
            return d.into_basket() # and here
            print(f'there are no {d.into_basket()} in here. ') # and here again.

d = Fruit()
e = Basket()


It is a little bit silly and over easy. But it is working.
See the comments in code for explanation how it goes into next class.
I hope this explain how to use other classes in the “main class”


I am working on my game, and I have got each room in a different class, and each class in a different file. I can’t work out how to move from room to room eg.

if yes (#go to this room)

elif (#go to this room)


Hello @GK77

You need at least have the Engine and Map class in one file.
The rooms can be imported.

Build one at the time.

Alright, so first thing is a bit of terminology clean up. This is a class:

class A(object):

class B(object):

But, these are objects of those classes:

one_a = A()
one_b = B()

You want to know how to move between objects, not classes. Think of a class like the blueprints to one of those fabricated house. You don’t move into the blueprints. You move into one of the houses that was built from the blueprints. You would say, “How do I move from house #455 on Lowell Lane to house #344 on Franklin Ave?” You don’t ask, “How do I move from the blueprints for these houses to the blueprints for a cathedral.”

Hopefully that analogy makes sense. Now, to move based on the game there’s the enter method:

class Kitchen(object):
   def enter(self):
        print("Enter Kitchen")

class Bedroom(object):
    def enter(self):
        print("Enter Bedroom")

lower_kitchen = Kitchen()
upper_kitchen = Kitchen()
master_bedroom = Bedroom()
kids_room = Bedroom()

Let’s say I have a house with these rooms in it. If I want to go into the master_bedroom I do this:


Now, let’s say I want someone who is in the master_bedroom to go into the lower_kitchen. Then, I have two choices:

  1. Inside the Bedroom.enter I just call lower_kitchen.enter().
  2. I make a map, and each room returns the name of the next room.

Now, with that information go study the code again. Also, try making a 1 room game with the engine working then just add your room. Build up from mine, don’t start fresh with your own design.

1 Like

Thanks a lot for this explanation @zedshaw.

I still want to fully understand everything in this game.
And this is one of the last missing pieces in the jig saw puzzle.
The row: “master_bedroom.enter()”
just made that piece come into place.
I knew I needed it. But it just wasn’t clear why until now.
My picture cleared considerably with this analogy.

I also would like to know if all the room having “Scene” inside brackets will inherit exit(1) from the Scene class?


Yes, if you have (Scene) then this class picks up everything from Scene. Think of the class in () as saying, “I am making a new type of Scene that has all the same things EXCEPT:” Then the code you add is the difference.

Thanks Zed, I got my game working, and I was moving from object to object, when I started getting this error every time I tried to run the game:

from MsOffice import MsOffice

ImportError: cannot import name ‘MsOffice’

I don’t understand why this suddenly happened

If you made the MsOffice.py file then you’ll have to make sure it’s in the right location and that you can import it. If this is from a module you installed with pip then make sure you have activated your virtualenv so you have access to it.

MsOffice is in the correct location, I haven’t moved it. I think the problem started when I tried to ask ‘Do you want to play again?’ and tried to take the player back to the original object

Ok, so how are you trying to figure this out? Are you just staring at the code or are you debugging it? Debugging means printing out the variables, or you can try using this:


I’ve tried using a debugger, but it just stops at the first line, so does the game

I’ve got the game working again, I think I was having problems with circular imports.
I ask “Do you want to play again?”
If the answer if yes, I need to take the player back to the beginning of the game - the first object, I am not sure how to do this, if I type "(objectname).enter() in the if statement after “yes”, I get a name not defined error

Well you have to make sure that variable is available there. Also why are you doing (objectname).enter() instead of objectname.enter()?

I have tried to import the object, but then I can’t start the game at all, as I get an

cannot import name …


Attach a screenshot here. But remember these rules:

  1. If you put FooBar class inside mymodule.py, then your import is:

from mymodule import FooBar

  1. If you can’t do ls mymodule.py and list it, then you are not in the right directory.
  2. If you put mymodule.py in a directory, like theapp/mymodule.py, then your import is now:

from theapp.mymodule import FooBar
4. Just like #2 if you can’t do ls theapp/mymodule.py then it won’t work. You have to be in the directory that lets you do read/write that file. The mistake everyone makes is:

python ../code.py

Do not cd into the directories. Stay above them.

I’ve uploaded 3 screenshots of the 3 files that are interlinked.

Everything works, I just can’t get from the BondGirl object to the MoneyPenny object, when the player decides to play again.

Great, glad you got it working. Did you know you can do an account for free at https://github.com/ and then post your whole project there.