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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Dylan Merritt](https://secure.gravatar.com/avatar/08759ac4ac6019f45d1242dab0d1b2e2?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dylan Merritt
15,682 PointsMy moves aren't being recognized as variables. I've checked it against the code in the video multiple times but am stuck
import random
CELLS = [(0,0), (0,1), (0,2),
(1,0), (1,1), (1,2),
(2,0), (2,1), (2,2)]
def get_locations():
monster = random.choice(CELLS)
door = random.choice(CELLS)
start = random.choice(CELLS)
if monster == door or door == start or monster == start:
return get_locations()
return monster, door, start
def move_player(player, move):
x,y = player
if move == 'LEFT':
y -= 1
elif move == 'RIGHT':
y += 1
elif move == 'UP':
x += 1
elif move == 'DOWN':
x -= 1
return x,y
def get_moves(player):
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player[1] == 0:
moves.remove('LEFT')
if player[1] == 2:
moves.remove('RIGHT')
if player[0] == 0:
moves.remove('UP')
if player[0] == 2:
moves.remove('DOWN')
return moves
print("Welcome to the dungeon!")
monster, door, player = get_locations()
while True:
moves = get_moves(player)
print("You are currently in room {}.".format(player))
print("You can move {}.".format(moves))
print("Enter QUIT to quit.")
move = input("> ")
move = move.upper()
if move == 'QUIT':
break
if move in moves:
player = move_player(player, move)
else:
print("***Walls are hard, quit running into them!***")
continue
if player == door:
print("You escaped!")
break
elif player == monster:
print("You were too young to die!")
break
1 Answer
![Chris Freeman](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 PointsUp is down and Down is up. The math is reversed on your UP and DOWN. Up should subtract 1 to get to lower numbered row. Down should add 1 to get to a higher numbered row:
def move_player(player, move):
x, y = player
if move == 'LEFT':
y -= 1
elif move == 'RIGHT':
y += 1
elif move == 'UP':
x -= 1 # <-- changed to subtract 1
elif move == 'DOWN':
x += 1 # <-- changed to add 1
return x, y
After this change, it appears to work as intended.