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

Instance Methods

when ever i try to create a new character its doesnt let me input name and weapon. What's wrong with my code?

class Character:
  xp = 0
  hp = 10

  def getweapon(self):
    weaponchoice = input("Weapon ([S]word, [A]xe, [B]ow): ").lower()
    if weaponchoice in 'sab':
      if weapon =='s':
        return 'sword'
      elif weapon =='a':
        return 'axe'
      else:
        return 'bow'
    else:
      return self.getweapon()

  def __intit__(self, **kwargs):
    self.name = input('Name: ')
    self.weapon = self.getweapon()

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

2 Answers

Looks like you have a typo. Your code says intit, rather than __init:

def intit(self, **kwargs):

ok so i fixed that so it asks me for name but when i try to select a weapon using s a or b it gives me the error weapon is not defined

You haven't defined weapon anywhere in your code.

In your getweapon method you have only defined weaponchoice. You've then gone from using 'weaponchoice' to just 'weapon'.

Either use weaponchoice throughout, or change occurences of weaponchoice to weapon, and it should work.