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 trialMaciej Walczak
5,042 PointsNameError: name 'get_weapon' is not defined
So i'm trying to get this code to work, but after it executes i get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/treehouse/workspace/character.py", line 21, in __init__
self.weapon = self.get_weapon()
File "/home/treehouse/workspace/character.py", line 16, in get_weapon
return self.get_weapon()
NameError: name 'get_weapon' is not defined
class Character(object):
experience = 0
hit_points = 10
def get_weapon(self):
weapon_choice = input("Choose thy 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"
else:
return "bow"
else:
return self.get_weapon()
def __init__(self, **kwargs):
self.name = input("What is thy name? ")
self.weapon = self.get_weapon()
for key, value in kwargs.items():
setattr(self, key, value)
Whenever i input the "right" string in "sab" i get this:
<character.Character object at 0x7f527547bdd8>
2 Answers
Maciej Walczak
5,042 Pointsok, i know what i did wrong. I took a shotcout. Didnt create an instance of Character f.e. player = Character (). After importing character i just called Character() class. When i created an instance, everything worked aye ok.
Martin Cornejo Saavedra
18,132 PointsIt worked fine for me. I created a character, and whenever I entered a wrong weapon, it asked again the question "Choose thy weapon...".