Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dungeon Game Building the Game: Part 1

Tab Error on Dungeon Game

Whenever I try to run my game I get this error and I am unable to fix it? Any help is appreciated, thanks.

File "dungeon_game.py", line 12
if monster == door or monster == start or door == start:
^
TabError: inconsistent use of tabs and spaces in indentation

Paste your code here, please.

The following is my code.

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)# monster = random door = random.choice(CELLS)# door = random start = random.choice(CELLS)# start = random if monster == door or monster == start or door == start: return get_locations() # return monster, door, start 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'] # player = (x, y) if player[1] == 0: moves.remove('LEFT')# if player's y is 0, remove LEFT if player[1] == 2: moves.remove('RIGHT')# if player's y is 2, remove RIGHT if player[0] == 0: moves.remove('UP')# if player's x is 0, remove UP if player[0] == 2: moves.remove('DOWN')# if player's x is 2, remove DOWN

return moves

monster, door, player = get_locations()

while True: moves = get_moves(player) print("Welcome to the dungeon!") print("You're currently in room {}".format(player)) # fill in with player position 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 it's a good move, change the player's position
   # If it's a bad move, don't change anything
  if move in moves:
          player == move_player(player, move)
  else:
        print("** Walls are hard stop walking into them! **")
        continue
    # If the new player position is the door, they win!
  if player == door:
        print("You have escaped! You Win!")
        break
# If the new player position is the monster, they lose!
  elif player == monster:
        print("Oh oh! The monster has caught you! You Lose!")
        break   
# Otherwise, continue

+Kenneth Love would you be able to help with this please?

5 Answers

Yea that happens when posting here. So, it is possible that you may have, at separate times, used tab to indent and space to indent. This, in my experience, causes this error. Unfortunately, it is a pain. Highlight all of your code in your workspace, and click 'ctrl+]' until all of your code is left aligned, then you're going to re-indent ONLY using either space or tab. Tab is more convenient.

Could you delete your code post, and re-post with proper markdown? This is not legible. Post this way:

<your code here>

The following is my code.

¬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)# monster = random door = random.choice(CELLS)# door = random start = random.choice(CELLS)# start = random if monster == door or monster == start or door == start: return get_locations() # return monster, door, start 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'] # player = (x, y) if player[1] == 0: moves.remove('LEFT')# if player's y is 0, remove LEFT if player[1] == 2: moves.remove('RIGHT')# if player's y is 2, remove RIGHT if player[0] == 0: moves.remove('UP')# if player's x is 0, remove UP if player[0] == 2: moves.remove('DOWN')# if player's x is 2, remove DOWN

return moves monster, door, player = get_locations()

while True: moves = get_moves(player) print("Welcome to the dungeon!") print("You're currently in room {}".format(player)) # fill in with player position 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 it's a good move, change the player's position

# If it's a bad move, don't change anything if move in moves: player == move_player(player, move) else: print("** Walls are hard stop walking into them! **") continue # If the new player position is the door, they win! if player == door: print("You have escaped! You Win!") break

If the new player position is the monster, they lose!

elif player == monster: print("Oh oh! The monster has caught you! You Lose!") break

Otherwise, continue¬

Sorry I am not sure how to post my code with the proper markdown is there a way to do it?

Ooop...ha sorry. I wrote the instructions but the markdown formatted my post.

If you go to the bottom of the box where you are typing, you'll see a markdown cheatsheet link. Click it.

Thanks :)

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)# monster = random
    door = random.choice(CELLS)# door = random
    start = random.choice(CELLS)# start = random
      if monster == door or monster == start or door == start:
            return get_locations()
       # return monster, door, start
    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']
       # player = (x, y)
      if player[1] == 0:
            moves.remove('LEFT')# if player's y is 0, remove LEFT
      if player[1] == 2:
        moves.remove('RIGHT')# if player's y is 2, remove RIGHT
    if player[0] == 0:
        moves.remove('UP')# if player's x is 0, remove UP
    if player[0] == 2:
        moves.remove('DOWN')# if player's x is 2, remove DOWN

    return moves

monster, door, player = get_locations()

while True:
      moves = get_moves(player)
    print("Welcome to the dungeon!")
    print("You're currently in room {}".format(player))  # fill in with player position
    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 it's a good move, change the player's position
       # If it's a bad move, don't change anything
      if move in moves:
              player == move_player(player, move)
      else:
            print("** Walls are hard stop walking into them! **")
            continue
        # If the new player position is the door, they win!
      if player == door:
            print("You have escaped! You Win!")
            break
    # If the new player position is the monster, they lose!
      elif player == monster:
            print("Oh oh! The monster has caught you! You Lose!")
            break   
    # Otherwise, continue

Note: Some of the IF statements look incorrectly indented however in the actual cod efile they are fine.

it's either ctrl+] or ctr+[ it's been a while since i have used workspaces.

sorry for all the quick responses, but i would start with line 12 first and see if that is the only indentation problem

Thanks very much that fixed my problem! :D