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

Adding monsters

I tried adding monsters to the display and got an indent error (unexpected indent on for idx...). The commented out code works, but my code with the monsters gets the error.

"""  
  for idx, cell in enumerate(CELLS):
    if idx in [0,1,3,4,6,7]:
      if cell == player:
        print(tile.format('X'), end='')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      else:
        print(tile.format('_|'))
"""        
# Now try the above code with monsters added  
  for idx, cell in enumerate(CELLS):
    if idx in [0,1,3,4,6,7]:
      if cell == player:
        print(tile.format('X'), end='')
      elif cell == monster:
        print(title.format('!'), end = '')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      elif cell == monster:
        print(title.format('!|'))
      else:
        print(tile.format('_|'))

monster,door,player = get_locations()
print("Welcome to the dungeon!")

13 Answers

There is a typo in one of your print statements. You typed in "print(title. ...) instead of print(tile. ...).

It is because your code block that starts with IDX is one space from the left, shift tab the entire code block and see if that works!!!

The 2 blocks of code have the same indentation, and the one above (that is commented out now), works. I don't see any code that starts with IDX, are you referring to the line that starts with for idx? At any rate, I doesn't seem that indentation could be the issue.

Yes the first line of the code block... for idx. That line starts one character space from the left where your block, beginning with monster, starts with no indents. Python is whitespace dependent and expects consistency as well so it expects all code blocks to start at the same level of indentation. The code in the comments returns the same error if you run it with another code block below it starting with no indentation.

for idx, cell in enumerate(CELLS):
  if idx in [0,1,3,4,6,7]:
    if cell == player:
      print(tile.format('X'), end='')
    elif cell == monster:
      print(title.format('!'), end = '')
    else:
      print(tile.format('_'), end='')
  else:
    if cell == player:
      print(tile.format('X|'))
    elif cell == monster:
      print(title.format('!|'))
    else:
      print(tile.format('_|'))

monster,door,player = get_locations()
print("Welcome to the dungeon!")

so we can see that the code above was modified to the for idx line has the same indentation as the line beginning with monster, ... this will not give you an indentation error

This code is part of a larger function that starts with def draw_map(player): which is identical to the code in Kenneth's lesson. The block of code that I commented out works fine and is indented correctly. The second block of code was an attempt to modify the commented out code by adding the monster to the diagram. It needs to start out with a 2 space indent, just as the commented out code above it did. Neither of these blocks should be all the way to the left margin, lined up with the monster line at the bottom, because the starting line (def draw_map(player):) is lined up there.

which video is this?

Python collections/Dungeon Game/Building the Game:Part 2

ok I did the workspace using your code and got no indentation errors. what error I did get though is calling the print functions that you put in. Instead of the object tile.format you put title.format and it returned an error

My print statements work just fine in the commented out code above. I added 2 elif statements in between the if and else statements. I suspect my "unexpected indent" error has something to do with them.

I used your code in the same workspace and got no indentation errors i just got the errors from the title being undefined

Here is all of 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
  # door = random
  # start = random
  monster = random.choice(CELLS)
  door = random.choice(CELLS)
  start = random.choice(CELLS)

  #if monster, door, or start are the same, do it again
  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):
  # player = (x,y)
  x,y = player
  # Get the player's current location
  # if move is LEFT, y-1
  # if move is RIGHT, y+1
  # if move is UP, x-1
  # if move is DOWN, x+1
  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's y is 0 , remove LEFT
  if player[1] == 0:
    moves.remove('LEFT')
  # if player's x is 0, remove UP
  if player[0] == 0:
    moves.remove('UP')
  # if player's y is 2, remove RIGHT
  if player[1] == 2:
    moves.remove('RIGHT')
  # if player's x is 2, remove DOWN
  if player[0] == 2:
    moves.remove('DOWN')
  return moves

def draw_map(player):
  print(' _ _ _')
  tile = '|{}'

# The commented out code below makes the program run just as expected  
"""
  for idx, cell in enumerate(CELLS):
    if idx in [0,1,3,4,6,7]:
      if cell == player:
        print(tile.format('X'), end='')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      else:
        print(tile.format('_|'))
"""   

# Now try the above code with monsters added, and I get an indent error (unexpected indent) 

  for idx, cell in enumerate(CELLS):
    if idx in [0,1,3,4,6,7]:
      if cell == player:
        print(tile.format('X'), end='')
      elif cell == monster:
        print(title.format('Z'), end = '')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      elif cell == monster:
        print(title.format('Z|'))
      else:
        print(tile.format('_|'))

monster,door,player = get_locations()
print("Welcome to the dungeon!")

while True:
  moves = get_moves(player)
  print("You're currently in room {}".format(player))  # fill in with player position
  draw_map(player)
  print("  ")
  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 walking into them! **")
    continue

  if player == door:
    print("You escaped!")
    break
  elif player == monster:
    print("You were eaten by the grue!")
    break         

    # if it's a good move, change the players position
    # if it is a bad move, don't change anything
    # if the new player position is the door, they win!
    # if the new player position is the monster, they lose!
    # Otherwise, continue

I just took out the big commented out block inside draw_map, and the indent errors went away. Apparently it does not like having a commented out section inside of a function, or maybe it needed the comment symbols # and """ to be indented as well.

I also fixed my typos on the word "title".

Now it is crashing on my new code. Perhaps I should just drop this. I wanted to see if I could modify the code to add the location of the monster to the grid. I just need someone to look at this section of code and tell me why it does not work. If it is not obvious to someone, that's ok, I will just move on.

for idx, cell in enumerate(CELLS):
  if idx in [0,1,3,4,6,7]:
    if cell == player:
      print(title.format('X'), end='')
    elif cell == monster:
      print(title.format('Z'), end = '')
    else:
      print(tile.format('_'), end='')
  else:
    if cell == player:
      print(title.format('X|'))
    elif cell == monster:
      print(title.format('Z|'))
    else:
      print(tile.format('_|'))

You still have a typo in your code. It needs to be tile.format not title.format. You have that same typo in 4 lines of your code

That worked, thanks! I was getting the words tile and title mixed up. At some point along the way, I thought I needed title. It is working now! Thanks for sticking with me :-)