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

NameError: name 'Mike' is not defined

I am trying to figure out why my game is not working. I run the code below and I get the following error:

Traceback (most recent call last):
File "game2.py", line 110, in <module>
Game()
File "game2.py", line 92, in init
self.setup()
File "game2.py", line 10, in setup
self.player = Character()
File "/home/treehouse/workspace/character.py", line 33, in init
self.name = input("Name: ")
File "<string>", line 1, in <module>
NameError: name 'Mike' is not defined

Here is my game.py code:


import sys from character import Character from monster import Dragon from monster import Goblin from monster import Troll

class Game: def setup(self): self.player = Character() self.monsters = [ Goblin(), Troll(), Dragon(), ] self.monster = self.get_next_monster()

def get_next_monster(self):
    try:
        return self.monsters.pop(0)
    except IndexError:
        return None

def monster_turn(self):
    # Check to see if the monster attacks
    # If so, tell the player
    if self.monster.attack():
        print('{} is attacking!'.format(self.monster))
        # Check to see if the player wants to dodge
        if input("Dodge? Y/N ").lower() == 'y':
            # If so, see if the dodge is successful. If it is, move on.
            if self.player.dodge():
                print('You dodged the attack!')
            # If it isn't lower hit points by one
            else:
                print('You got hit anyways!')
                self.player.hit_points -= 1
        else:
            print('{} hit you for 1 point!'.format(self.monster))
            self.player.hit_points -= 1
    # If the monster isn't attacking, tell that to the player        
    else:
        print("{} isn't attacking this turn.".format(self.monster))

def player_turn(self):
    # Let the player attack, rest, or quit
    player_choice = input('[A]ttack, [R]est, or [Q]uit? ').lower()
    # If they attack:
    if player_choice == 'a':
        print("You're attacking {}!".format(self.monster))
        # See if the attack is successful
        if self.player.attack():
            # If so, see if the monster dodges
            if self.monster.dodge():
                # If dodged, print that
                print('{} dodged your attack!'.format(self.monster))
            else:
                # If not dodged, subtract the right number of hit points from the monster
                if self.player.leveled_up():
                    self.monster.hit_points -= 2
                else:
                    self.monster.hit_points -= 1
                print("You hit {} with your {}!".format(self.monster, self.player.weapon))
        else:
            # If not a good attack, tell the player
            print('You missed!')

    # If they rest:
    elif player_choice == 'r':
        # Call the player.rest method
        self.player.rest()

    # If they quit, exit the game
    elif player_choice == 'q':
        sys.exit()

    # If they pick anything else, rerun this method
    else:
        self.player_turn()

def cleanup(self):
    # If the monster has no more hit points:
    if self.monster.hit_points <= 0:
        # Up the player's experience
        self.player.experience += self.monster.experience
        # Print the message
        print("You killed {}!".format(self.monster))
        # Get a new monster
        self.monster = self.get_next_monster()   

def __init__(self):
    self.setup()

    while self.player.hit_points and (self.monster or self.monsters):
        print('\n'+'='*20)
        print(self.player)
        self.monster_turn()
        print('-'*20)
        self.player_turn()
        self.cleanup()
        print('\n'+'='*20)

    if self.player.hit_points:
        print('You win!')
    elif self.monsters or self.monster:
        print('You lose!')
    sys.exit()

Game()


Here is my character.py code:


import random

from combat import Combat

class Character(Combat): attack_limit = 10 experience = 0 base_hit_points = 10

def attack(self):
    roll = random.randint(1, self.attack_limit)
    if self.weapon == 'sword':
        roll += 1
    elif self.weapon == 'axe':
        roll += 2
    return roll > 4

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()
    self.hit_points = self.base_hit_points

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

def __str__(self):
    return '{}, HP: {}, XP: {}'.format(self.name, self.hit_points, self.experience)

def rest(self):
    if self.hit_points < self.base_hit_points:
        self.hit_points += 1

def leveled_up(self):
    return self.experience >= 5

3 Answers

Actually, I just did a bunch of searching and found out that my python on treehouse was defaulting to Python 2.7. I fixed it by typing python3 first. Why would this switch? It was defaulting to 3.5 before!

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

The switch of python pointing to python2 instead of python3 is a new bug. They are working on restoring it back to python3. Should be fixed soon.

As a workaround use python3, or use a quoted string "Mike" for python 2 input() function.

Awesome thanks Chris!