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
Diego Salas Polar
21,431 PointsI 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
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsHi 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
21,431 PointsDiego Salas Polar
21,431 PointsThank 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
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsThe
__str__(),rest(), andleveled_up()methods incharacter.pyhave incorrect indentation. I suggest increasing to the standard 4-space indentation to help see the misalignment.Diego Salas Polar
21,431 PointsDiego Salas Polar
21,431 PointsThank 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
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsTo find the bug I would use the python debugger
pdb. You can run it using module switch to Python:$ python -m pdb game.pyThis will put you in the python interpreter with pdb running. Some basic commands are:
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
21,431 PointsDiego Salas Polar
21,431 PointsOkay but thank you for help me out and fixing the game. Thanks :D