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 Object-Oriented Python (retired) Hack-n-Slash The Final Push

igsm '
igsm '
10,440 Points

AttributeError: 'Game' object has no attribute 'monster'.

Hi! Can't figure out what's wrong, please help. I get the following error:

Traceback (most recent call last):
  File "C:\Users\Dinosaur\Desktop\Treehouse\game.py", line 99, in <module>
    Game()
  File "C:\Users\Dinosaur\Desktop\Treehouse\game.py", line 82, in __init__
    self.setup()
  File "C:\Users\Dinosaur\Desktop\Treehouse\game.py", line 12, in setup
    self.monster = self.get_next_monster()
  File "C:\Users\Dinosaur\Desktop\Treehouse\game.py", line 16, in get_next_monster
    return self.monster.pop(0)
AttributeError: 'Game' object has no attribute 'monster'
import sys

from Character_Class import Character
from monster import Dragon
from monster import Goblin
from monster import Troll

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

    def get_next_monster(self):
        try:
            return self.monster.pop(0)
        except IndexError:
            return None

    def monster_turn(self):
        # Check to see if the monster attacks
        if self.monster.attack():
        #if so, tell the player
            print '{} is attacking!'.format(self.monster)
            #check if the player wants to dodge
            if raw_input('Dodge? Y/N ').lower() == 'y':
                #if so, see if the dodge is successful
                if self.player.dodge():
                    #if it is, move on
                    print 'You dodged the attack!'
                #if it's not, remove one player hit point
                else:
                    print 'You got hit anyway!'
                    self.player.hit_points -= 1
            else:
                print '{} hit you for 1 point!'.format(self.monster)
                self.player.hit_points -= 1
        #if the monster isn't attacking, tell that to the player too
        else:
            print '{} is not attacking this turn.'.format(self.monster)

    def player_turn(self):
        #Let the player attack, rest, or quit
        player_choice = raw_input('[A]ttack', '[R]est', '[Q]uit').lower()
        #if they attack:
        if player_choice == 'a':
            print 'Yor are attacking {}!'.format(self.monster)
            #See if the attack is successful
            if self.player.attack():
                #if so, see if the monster dodges
                if self.monster.dodge():
                    #if  dodged, print that
                    print '{} dodged your attack!'.format(self.monster)
                #If not dodged, substruckt the right num of hit points from the monster
                else:
                    if self.player.leveled_up():
                        self.monster.hit_points -= 2
                    else:
                        self.monster.hit_points -= 1

                    print 'You hit {} with your {}!'.format(self.monster, self.player.weapon)
            else:
                print 'You missed!'
        #if they rest:
        elif player_choice == 'r':
            #call the player.rest() method
            self.player.rest()
        #if they quit, exit the game    
        elif player_choice == 'q':
            sys.exit()
        #if they pick anything else, re-run this method
        else:
            self.player_turn()

    def cleanup(self):
        if self.monster.hit_points <= 0:
            self.player.experience += self.monster.experience
            print 'You killed {}!'.format(self.monster)
            self.monster = self.get_next_monster()

    def __init__(self):
        self.setup()

        while self.player.hit_points and (self.monster or self.monsters):
            print '\n' + '=' * 20
            print self.player
            self.monster_turn()
            print '-' * 20
            self.player_turn()
            self.cleanup()
            print '\n' + '=' * 20

        if self.player.hit_points:
            print 'You Win!'
        elif self.monsters or self.monster:
            print 'You lose!'
        sys.exit()

Game()

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You have self.monster.pop when you should have self.monsters.pop. Notice that the second one is pluralized.

igsm '
igsm '
10,440 Points

Thank you.