Ex52 planisphere_tests.py

Most of the time if you get a NameError it’s because:

  1. You spelled something wrong, either where you use it or where you create the variable.
  2. You didn’t really create the variable like you though.
  3. It’s in a different place than you think. So you’re doing load_room but you have it on an object’s self, so need to do self.load_room.
1 Like

start_room = load_room(START)
NameError: name ‘load_room’ is not defined

is the above code correct?
thank you for the help

)
NameError: name ‘self’ is not defined

Sorry to be such a pain!

Hi @ailsagy can you post the code for the class load_room? We would understand better what is going on. Put your code in brackets like this:

[code]
Your code
[/code]

Another thing would be that you go back to earlier exercises in the book (EX40 to 45) and try to relearn some basic concepts about OOP.

I’m sorry @ailsagy but you’re still giving everyone not enough information to help you. There’s simply no way to figure out what’s going on with only a single screenshot and tiny single lines of information. Even if you posted all of the code, you’re still coming and asking for help but not really doing any of the work you need to do before you ask for help.

Please go and figure this out on your own first. You should know what self is by this exercise. You should know how to find a NameError. Based on that I suspect you also have been skipping through the book and not carefully studying. It might help you to start at the beginning and refresh what you’ve learned so far.

Finally, I would really love to know why you post so little information. Is it a language problem? You don’t speak English well enough to write up your help request? Are you embarrassed to ask for help? Maybe if you told me more I could help you.

This is my code for planisphere.py class Room(object):

def __init__(self, Room, start_room = load_room):
    self.Room = Room
    self.start_room = start_room
    self.load_room = load_room
    self.paths = {}

def go(self, direction):
    return self.paths.get(direction, None)

def add_paths(self, paths):
    self.paths.update(paths)

Traceback (most recent call last):
File “C:\Users\Administrator.venvs\lpthw\lib\site-packages\nose\case.py”, line 198, in runTest
self.test(*self.arg)
File “C:\Users\Administrator\projects\gothonweb\planisphere_tests.py”, line 34, in test_gothon_game_map
start_room = load_room(START)
NameError: name ‘load_room’ is not defined
but when I run nosetests I get the following error message:

Hi @ailsagy it’s not all code, but maybe it is enough to figure out the problem.
You use the variable load_room in the function definition of __init__

def __init__(self, Room, start_room = load_room):
# load_room is assigned before you define it in the function body
self.load_room = load_room

But you define the variable load_room after that assignment. So Python doesn’t know yet that variable. That is what the error: NameError: name ‘load_room’ is not defined

1 Like

thank you for your help!

There you go, your init line is the problem:

def __init__(self, Room, start_room = load_room):

You have a class named Room (capitalization matters A LOT), so you’re going to have major problems if you then have a variable named Room as well. Every thing gets a new name and capital letters are important, so Room is the same as Room but different from room.

Next, when you do start_room = load_room you’re saying “I want start_room to default to the variable load_room” but load_room isn’t defined anywhere. Then later on you try to use load_room. I suspect you actually meant:

def __init__(self, room, start_room, load_room):

But you also aren’t very clear in why you need all of this, which leads me to my next suggestion:

  1. Erase this init code.
  2. Write out English comments describing what your new init should do:
# get the room to start in
# save the paths
# get the room to load
  1. Write the python under those comments to make them do what you want. Take the time to work out your English comments so they make sense too. I say this because what you wrote in Python is kind of like what I’d read if someone didn’t know English too well and wrote:

“I have a sandwich cheese that uncovers you love it yes?”

  1. Write all of your code like this until you become fluent. This can be years, AND I still do a lot of my code like this so that I can think in English then figure out what to write to make the English happen.

Hi @ailsagy,

I am not sure if you resolved your problem yet. I was getting the same error message that you did and it took me a while to figure out why that was. I typed all of the code as in the book, however I forgot to change the following:

from gothonweb.planisphere import Room
to
from gothonweb.planisphere import *

As the result, it was not importing all of the functions after the Room class and it was not able to execute them, after that change everything worked. Hope this helps!