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

Can someone spot any errors with this code:

class Character:
  experience = 0
  hitpoints = 10

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

    if weaponChoice in 'sab':
      if weaponChoice == 's':
        return 'sword'
      elif weaponChoice == 'a':
        return 'axe'
      else:
        return 'bow'
    else:
      return weaponChoice.getWeapon()

  def __init__(self, **kwargs):
    self.name = input("Name: ")
    self.weapon = self.getWeapon()

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

Seems like I get an error when I have the wrong input, when it should just return the method

The return statement in the second else statement is pointing to "weaponChoice.getWeapon()". However, the local variable weaponChoice does not have a method called getWeapon(). You should return self.getWeapon() instead.

1 Answer

Hello Tristan.

I only had a quick look at the code as I am unable to test it at the moment, but shouldn't it just be self.getWeapon() instead of weaponChoice.getWeapon() ?

What do you want to achieve? In the main if/else statement; do you want to do the process again if weaponChoice is NOT in 'sab'?

If that is the case, you only need to call the method again with (I think) self.getWeapon()

Vittorio

Thanks, that was my problem!