Most of the time if you get a NameError itâs because:
- You spelled something wrong, either where you use it or where you create the variable.
- You didnât really create the variable like you though.
- 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.
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
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:
- Erase this init code.
- 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
- 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?â
- 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!