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 Object-Oriented Python (retired) Inheritance Instance Methods

getting NoneType weapon once input invalid weapon letter

Hi, I think i have the same code as Ken. And once I input 's' 'a' 'b', i will get the right result. however, else letters will get me None instead of repeating input for weapon. Can someone have a look, thx

class C: exp = 0 points = 10 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'
  elif weapon_choice == 'a':
    return 'axe'
  elif weapon_choice == 'b':
    return 'bow'
  else:
    print('invalid, try it again')
    return self.get_weapon()

def init(self, **kwargs): self.name = input('name : ') self.weapon = self.get_weapon()

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

from character import C
az = C()
name : JL
weapon : [S]word, [A]xe, [B]ow > q
az.name
'JL'
az.weapon
type(az.weapon)
<class 'NoneType'>

1 Answer

Steven Parker
Steven Parker
229,732 Points

Unfortunately, without proper blockquoting in the code above, the actual indentation is not clear. And indentation is crucial in Python. And I suspect that you have an indentation-related issue.

It looks like the "else" in this part of the code:

  else:
    print('invalid, try it again')
    return self.get_weapon()

...might line up with the "if" six lines above in "if weapon_choice == 's':".

:point_right: But it should line up with the "if" in this line instead:

    if weapon_choice in 'sab':

For future reference, check the Markdown Cheatsheet pop-up below for info on quoting code. :arrow_heading_down:

thanks Steven! this helped =)