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

Diego Salas Polar
Diego Salas Polar
21,431 Points

I need help with this attribute error with my game. I can't figure out a way to solve this problem. Please help me!!

Here is the link to my software

https://w.trhou.se/a1sr8eccio

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Hi Diego, when running your code I got the following error:

treehouse:~/workspace$ python game.py
Name: Chris
Weapon [S]word, [A]xe, [B]ow: s

====================
<character.Character object at 0x7f149b5b3e48>
Traceback (most recent call last):
  File "game.py", line 105, in <module>
    Game()
  File "game.py", line 93, in __init__
    self.monster_turn()
  File "game.py", line 31, in monster_turn
    if self.monster.attack():
AttributeError: 'function' object has no attribute 'attack'

Inspecting game.py, you have used self.monster for two purposes: the monster list and the current monster. There are parens missing from the call to get_next_monster. Without the parens, you are assigning the function itself not its return value. Fixing these below:

class Game:
    def setup(self):
        self.player = Character()
        self.monsters= [  # <-- renamed to monsters
            Goblin(),
            Dragon(),
            Troll()
        ]
        ##############################
        self.monster= self.get_next_monster() # <-- Added parens

    def get_next_monster(self):
        try:
            return self.monsters.pop(0)  # <-- reference monsters list
        except IndexError:
            return None

Once these were fixed, other errors were found. Check the indentation on your methods in character.py

Diego Salas Polar
Diego Salas Polar
21,431 Points

Thank you for helping and I fix the changes as you told me to, but I still have errors in my program. Please help me again. Thanks!! :D

The link to my program. https://w.trhou.se/vx32pjtw8z

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

The __str__(), rest(), and leveled_up() methods in character.py have incorrect indentation. I suggest increasing to the standard 4-space indentation to help see the misalignment.

Diego Salas Polar
Diego Salas Polar
21,431 Points

Thank you very much Chris. I fix the indentation and the error after that. Chris, I have a question. When I run my game when I pressed the letter a to attack the dragon, or the troll, or the goblin, it kept say "I win" Even if I pressed the letter r to rest and the monster haven't died yet. How would you fix that bug? Any thoughts you may have?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

To find the bug I would use the python debugger pdb. You can run it using module switch to Python:

$ python -m pdb game.py

This will put you in the python interpreter with pdb running. Some basic commands are:

  • n to advance to next statement
  • s to step into a function call
  • l to list the code surrounding the current instruction.

At the (pdb) prompt, you can inspect any variable by simply typing its name. All regular Interpreter functionality is also available.

See the pdb docs for more details.

Diego Salas Polar
Diego Salas Polar
21,431 Points

Okay but thank you for help me out and fixing the game. Thanks :D