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!

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


Green Teh
Green Teh
9,439 Points

I got the same error message but i can't find the mistake in my code. Need some help!

When i run player.attack(), i got the following error message:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/treehouse/workspace/character.py", line 11, in attack
if self.weapon == 'sword':
AttributeError: 'Character' object has no attribute 'weapon'

My code:

import random
from combat import Combat

class Character(Combat):
  attack_limit = 10
  experience = 0 
  base_hit_points = 10

  def attack(self):
    roll = random.randint(1, self.attack_limit)
    if self.weapon == 'sword':
      roll += 1
    elif self.weapon == 'axe':
      roll += 3
    return roll > 4

  def get_weapon(self):
    weapon_choice = input("Weapon ([S]word, [A]xe, [B]ow): ").lower()

    if weapon_choice in 'sab':
      if weapon_choice == 's':
        return 'sword'
      if weapon_choice == 'a':
        return 'axe'
        return 'bow'
      return self.get_weapon()

  def __init__(self, **kwargs):
    self.name = input("Name: ")
    self.wapon = self.get_weapon()

    for key, value in kwargs.items():
      setattr(self, key, value)

1 Answer

Michael Hulet
Michael Hulet
47,909 Points

It's because at that point, self.weapon hasn't been defined, so when you check to see what it is, it doesn't exist, and Python crashes. This is just because you misspelled it in your __init__ (you wrote self.wapon instead of self.weapon). As soon as you fix that, the crash should go away

Green Teh
Green Teh
9,439 Points

Now i get it, thanks a lot Michael =)