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

Final game on python object oriented, Can not get it to work

Error that I keep getting is: AttributeError: 'Game' object has no attribute 'player_hit_points'

"""


========================================================================================================================

Note: (AttributeError: 'Game' object has no attribute 'player_hit_points') line 86 find solution for this. 

========================================================================================================================


"""

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 = self.get_next_monster()

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

    def monster_turn(self):
        if self.monster.attack():
            print("{} is attacking".format(self.monster))

            if input("Dodge? Y/N".lower() == 'y'):
                if self.player.dodge():
                    print("You dodged the attack!")
                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
        else:
            print("{} is not attacking this turn".format(self.monster))

    def player_turn(self):
        player_choice = input("[A]ttack. [R]est. [Q]uit".lower())
        if player_choice == 'a':
            if self.player.attack():
                print("You are attacking {}".format(self.monster))
                if self.monster.dodge():
                    print("{} dodged your attack!".format(self.monster))
                else:
                    if self.player.leveled_up():
                        self.monster.hit_points -= 2
                    else:
                        self.mosnter.hit_points -= 1
                    print("You hit {} with your {}".format(
                        self.monster, self.player.weapon))
            else:
                print("You Missed!")
        elif player_choice == 'r':
            self.player.rest()
        elif player_choice == 'q':
            sys.exit()
        else:
            self.player_turn()

    def cleanup(self):
        if self.monster.hit_points <= 0:
            self.player.experience += self.monster.experience
            print("You Killed the {}!".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()

The error is pointing to line 86

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You haven't included your Character class module so I can't be absolutely positive, but I feel like this is a typo. You've typed:

while self.player_hit_points and (self.monster or self.monsters):

I believe that should be:

while self.player.hit_points and (self.monster or self.monsters):

It seems that the player is an instance of the class Character and hit_points should be a stored property there. This means that we access it with dot notation.

Hope this helps! :sparkles:

Hello Jennifer,

You are correct, I kept over looking that. I also notice a lot of more typos with the monster variables. It is working now. Thanl you so much.