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 trialMohammed Jangda
1,701 PointsSyntax Error!
I get this error:
"Traceback (most recent call last):
File "dgame.py", line 57, in <module>
print("You're currently in room {}.".format(player)) # fill with player position
NameError: name 'player' is not defined "
this is my code:: import os import random
draw grid
pick a random location for the player
pick random location for exit door
pick random location for monster
draw player in the grid
take input for movement
move player, unless invalid move (out of the map)
check for win/loss
clear the screen and redraw grid
CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]
def clear_screen(): os.system("cls" if os.name == "nt" else "clear")
def get_locations(): return random.sample(CELLS, 3)
def move_player(player, move): # get the player's location # if move == LEFT, x-1 # if move == RIGHT, x+1 # if move == UP, y-1 # if move == DOWN, y+1 return player
def get_moves(player): moves = ["LEFT", "RIGHT", "UP", "DOWN"] x, y = player if x == 0: moves.remove("LEFT") if x == 4: moves.remove("RIGHT") if y == 0: moves.remove("UP") if y == 4: moves.remove("DOWN") # if players y == 0 they can't move up # if players y == 4 they can't move down # if players x == 0 they can't move left # if players x == 4 they can't move right return moves
while True: print("Welcome to the dungeon!") print("You're currently in room {}.".format(player)) # fill with player position print("You can move {}.".format(", ".join(get_moves(player)))) # fill with available move print("Enter QUIT to quit")
move = input("> ") move = move.upper()
if move == 'QUIT': break
Good move? Change the player position
Bad move? Don't change anything
On the door? They win!
On the monster? They lose!
Otherwise, loop back around
1 Answer
Steven Parker
229,644 PointsThat line of the program is trying to use the contents of a variable named "player" to print a message, but no variable with that name has been created yet in the program. Perhaps you missed a line while following along with the video where it was created?
Code formatting is extremely important in Python. To make sure your code can be seen as you wrote it, be sure to use "Markdown" formatting when you show code with questions. There's a pop-up "cheatsheet" below, or you can watch this video on code formatting to learn how.