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

Player not moving

Hi, I am following along in the Python Collections course and I am stuck after Building The Game Part 1. My player does not move. I am prompted with the positions I can move to and when I enter for example 'LEFT' then it does not move to the position I request. It's stay the same. If I enter a position to move that is not suggested than I get the message I expected. Here is my code, please explain what I did wrong here, I tried to do it myself but it did not go so well. :/

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 == start or monster == door or door == start:
    return get_locations()

  return monster, door, start


def move_player(player, move):
  x, y = player

  if move is 'LEFT':
    y -= 1
  elif move is 'RIGHT':
    y += 1
  elif move is 'UP':
    x -= 1
  elif move is 'DOWN':
    x += 1

  return x, y


def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player = (x, y)
  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')
  # if player's x is 0, remove UP
  # if player's x is 2, remove DOWN
  return moves

monster, player, door = get_locations()

while True:
  moves = get_moves(player)
  print("Welcome to the dungeon!")
  print("You're currently in room {}".format(player))  
  print("You can move {}".format(moves))  # fill in with available 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. Stop running in to them! ** ')
    continue

  if player == door:
    print('You escape the dungeon!')
    break
  elif player == monster:
    print("The monster got you :(")
    break

1 Answer

Steven Parker
Steven Parker
243,215 Points

:point_right: You're doing an identity comparison instead of an equality comparison.

In move_player, you have several tests using the identity comparator "is". This is always false because the variable and constant strings are never the same thing. But you really want to test if they have the same value, so replace those with the equality comparator "==".