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

getting a name error from my input

I am getting a name error from both of my input variables in my script and I am not sure why.

class Character:
    exp = 0
    hp = 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'
            else:
                return 'bow'
        else:
            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)

I set the self.name = "Luke" to pass that part, which then asked for the S A B input. However, upon entering S A or B I got a name error again.

>>> player = Character()                                                                                                                    
Weapon ([S]word, [A]xe, [B]ow): S                                                                                                           
Traceback (most recent call last):                                                                                                          
  File "<stdin>", line 1, in <module>                                                                                                       
  File "char.py", line 20, in __init__                                                                                                      
    self.weapon = self.get_weapon()                                                                                                         
  File "char.py", line 6, in get_weapon                                                                                                     
    weapon_choice = input("Weapon ([S]word, [A]xe, [B]ow): ").lower()                                                                       
  File "<string>", line 1, in <module>                                                                                                      
NameError: name 'S' is not defined

Same issue with self.name = input("Name: ")

Any help would be appreciated!

[MOD: aded ```python markdown formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

The issues is with Workspaces. python is pointing to python2 instead of python3. They are working on it and expect a fix soon. As a workaround, use python3 instead.

Python 2 interpret input differently. It tries to eval your input, so S is looked at like a variable name. In python2 use "S" (a quoted string") to input the character.

Thanks Chris! Working that way! Out of curiosity, is there a way to input a string in python2 without surrounding the string with quotes?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Yes. In py2 you can use raw_input(). In py3, raw_input() was renamed input(), and the py2 input() functionality was removed to replaced in py3 with eval(input()). But since running eval directly on user input is a very dangerous practice, it isn't used often (and why the py2 input() was removed in py3)

Makes sense. Thanks again for the help!