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 trialMarcin Mączewski
4,476 PointsNameError: name 'player' is not defined when choosing to [R]est
Like the title says. I'm getting this error whenever I'm trying to choose [R]est. Everything else but this works perfectly. This is the error I am getting:
[A]ttack, [R]est, [Q]uit? r
Traceback (most recent call last):
File "gamee.py", line 95, in <module>
Game()
File "gamee.py", line 82, in init
self.player_turn()
File "gamee.py", line 60, in player_turn
player.rest()
NameError: name 'player' is not defined
Here's my 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):
if self.monster.attack():
print("{} is attacking!".format(self.monster))
if input("Dodge? Y/N").lower() == 'y':
if self.player.dodge():
print("You dodged the attack!")
else:
print("You got hit anyway!")
self.player.hit_points -= 1
else:
print("{} hit you for 1 points!".format(self.monster))
self.player.hit_points -= 1
else:
print("{} isn't attacking this turn.".format(self.monster))
def player_turn(self):
player_choice = input("[A]ttack, [R]est, [Q]uit? ").lower()
if player_choice == 'a':
print("You're attacking {}!".format(self.monster))
if self.player.attack():
if self.monster.dodge():
print("{} dodged your attack!".format(self.monster))
else:
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:
print("You missed!")
elif player_choice == 'r':
player.rest()
print("You are resting.")
elif player_choice == 'q':
sys.exit()
else:
self.player_turn()
def cleanup(self):
if self.monster.hit_points <= 0:
self.player.experience += self.monster.experience
print("You killed {}!".format(self.monster))
self.monster = self.get_next_monster()
def __init__(self):
self.setup()
while self.player.hit_points and (self.monster or self.monster):
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()
1 Answer
Matthew Hill
7,799 PointsThink you've missed off the reference to self before the method call to rest in the player turn method, easily forgotten! Matt
Marcin Mączewski
4,476 PointsMarcin Mączewski
4,476 PointsThanks, that was it!