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 trialJude Molloy
7,470 PointsTab 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
Jude Molloy
7,470 PointsThe 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
Jude Molloy
7,470 Points+Kenneth Love would you be able to help with this please?
5 Answers
sradms0
Treehouse Project ReviewerYea 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.
sradms0
Treehouse Project ReviewerCould you delete your code post, and re-post with proper markdown? This is not legible. Post this way:
<your code here>
Jude Molloy
7,470 PointsThe 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¬
Jude Molloy
7,470 PointsSorry I am not sure how to post my code with the proper markdown is there a way to do it?
sradms0
Treehouse Project ReviewerOoop...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.
Jude Molloy
7,470 PointsThanks :)
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
Jude Molloy
7,470 PointsNote: Some of the IF statements look incorrectly indented however in the actual cod efile they are fine.
sradms0
Treehouse Project Reviewerit's either ctrl+] or ctr+[ it's been a while since i have used workspaces.
sradms0
Treehouse Project Reviewersorry for all the quick responses, but i would start with line 12 first and see if that is the only indentation problem
Jude Molloy
7,470 PointsThanks very much that fixed my problem! :D
sradms0
Treehouse Project Reviewersradms0
Treehouse Project ReviewerPaste your code here, please.