Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Problem with Monster game

So I typed the game in and thought I had it all correct. But when I run it in Terminal, it gives me "NameError: name 'get_next_monster' is not defined". Even though get_next_monster is the next thing in the file!

I know that order doesn't matter, unlike some languages.

I even tried cutting and pasting the rest of the code (what's not relevant to the problem) to another file just to eliminate problems as much as possible.

I also temporarily redefined the get_next_monster() method to simply print out a message, again to reduce any code that might be complicating things.

I have python 3.4 on my Mac (OS X 10.9.5), though I'm not sure if that has anything to do with it.

I just do not for the life of me understand what is wrong with this that is causing it to throw up errors. I thought I had typed it (well, other than what I changed afterwards) correctly, but it just refuses to work and keeps giving me the same error. Maybe it's something stupid and simple that I'm overlooking.

Thanks!

import sys
from character import Character
from monster import Dragon
from monster import Goblin
from monster import Troll


class Game:

    def setup(self):
        self.player = Character()
        self.monsters = [
            Goblin(), 
            Troll(), 
            Dragon() 
        ]
        self.monster = get_next_monster()

    def get_next_monster(self):
        print ('the next monster is up!')

    def __init__(self):
        self.setup()

Game()

and apparently when I tried to paste the code in, it didn't take it correctly, so part of my code is underneath the thanks. not sure what I did wrong there.

I have fixed your pasted code. Please see this thread about posting code for reference. :)

3 Answers

Hi Kevin Hower,

You're pretty close!

Line 17 is where the issue is:

self.monster = get_next_monster()
#should be:
self.monster = self.get_next_monster()

get_next_monster() needs to be referenced to self or we don't know where to pull that function from.

Thanks so much, Sean! Appreciate your time! That definitely got rid of that error.

I've got a few others to work through, but I think they are of a similar nature. Learning is hard, dang it ! Lol.

Again, thanks for the help!

Great News - after a few tweaks to fix some oversights on my part, I got the game to play! Woot!

Now, I am going to make a copy of it and make some upgrades! Thanks again, Sean Unwin!