Planisphere.py ex52.py

File “C:\Users\Administrator\Documents\lpthw\planisphere.py”, line 203, in
‘2’: the_end_winner,
NameError: name ‘the_end_winner’ is not defined

Hi @ailsagy.

Could you please provide some more information?
What did you run when this error occour?
The code is helpful too see.

If you start to write about when and where this happens you will perhaps find it yourself.
It’s a little magic trick Zed have taught us.

Any time you get that you have one of three mistakes:

  1. You spelled something wrong.
  2. You forgot to define that thing (it’s not in the file at all).
  3. You’re asking for it from the wrong place. Like it’s in one module but you try to get it from another.

Take a look at those.

1 Like
        good_pod = randint(1,5)
        guess = input("[pod #]> ")

        if int(guess) != good_pod:
            class the_end_loser(Room):
                def enter(self):
                    print(dedent("""
                          You jump into pod '*' and hit the eject button.
                          The pod escapes out into the void of space, then
                          implodes as the hull ruptures, crushing your body into
                          jam jelly.
                          """))
                    return 'death'
        else:
            class the_end_winner(Room):
                def enter(self):
                    print(dedent("""
                          You jump into pod '2' and hit the eject button.
                          The pod easily slides out into space heading to the
                          planet below. As it flies to the planet, you look
                          back and see your ship implode then explode like a
                          bright star, taking out the Gothon ship at the same
                          time. You won!
                          """))
                    return 'finished'

class Finished(Room):
    def enter(self):
        print ("You won! Good job.")
        return 'finished'

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

Hi @ailsagy

I guess this is not all of the code.
There must be something in the beginning that got lost when you put it up here.
I must say this one looks a little odd:

if int(guess) != good_pod: 
    class the_end_loser(Room): 
        def enter(self): 
            print(dedent(""" You jump into pod '*' and hit the eject button.....

Creating a class inside an if statement I have never seen before.
The rest of the code perhaps explain more.

Perhaps someone else can help with this too.
I am not that skilled I can say this ever will work or not.

1 Like

Wait, yes why are you defining classes inside if-statements? Take those and pull them up to the top of the file and then use them like the other classes are used. Also, you are just defining a class there so nothing actually runs. Since you have an if-statement around the classes that means that 50% of the time one of those classes won’t be defined.

Basically, it looks like you have a misconception about the definition of a class vs. the way you run a class’s code. Think of the definition like a definition in a dictionary. It’s put in one place and you can go look at it, BUT, when you use a word in a sentence you don’t copy the whole definition into your sentence right? Like if I wrote this:

I don’t have to include the whole definition of Honorificabilitudinitatibus in the sentence. I can just point you at the definition here:

What you did is something like this:

So, pull those class definitions out into the top, then you can use them like classes where you want.

Also, you should think through this code. I suspect that you’re typing code into there but not quite grasping how it works. Try this approach instead:

  1. Write English comments for what you want the code to do.
  2. Translate those to Python-like fake code as comments guessing at what should be written in Python.
  3. Write the python under those, and run it frequently to check that the lines you’re writing work.
  4. Delete the comments once the python is working.

This translates from English → Fake Python → Working Code better.