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
Scott Simontacchi
2,071 PointsDumb question: why do the X's not print?
Running the program before adding all the logging, the X prints, but after adding it the X doesn't print anymore (while the breadcrumbs do). What's the reason for this?
Sorry if this is obvious, I'm just not seeing it. Thanks!!
1 Answer
Scott Simontacchi
2,071 PointsHaha that makes sense! The video is on logging: https://teamtreehouse.com/library/write-better-python/buggy-logs/logging
And here is the code:
import logging
import random
logging.basicConfig(filename = 'game.log', level = logging.DEBUG)
player = {'location': None, 'path': []}
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 monster == start or door == start:
monster, door, start = get_locations()
return monster, door, start
def get_moves(player):
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
if player in [(0, 0), (1, 0), (2, 0)]:
moves.remove('LEFT')
if player in [(0, 0), (0, 1), (0, 2)]:
moves.remove('UP')
if player in [(0, 2), (1, 2), (2, 2)]:
moves.remove('RIGHT')
if player in [(2, 0), (2, 1), (2, 2)]:
moves.remove('DOWN')
return moves
def move_player(player, move):
x, y = player['location']
player['path'].append((x, y))
if move == 'LEFT':
player['location'] = x, y - 1
elif move == 'UP':
player['location'] = x - 1, y
elif move == 'RIGHT':
player['location'] = x, y + 1
elif move == 'DOWN':
player['location'] = x + 1, y
return player
def draw_map():
print(' _ _ _')
tile = '|{}'
for idx, cell in enumerate(cells):
if idx in [0, 1, 3, 4, 6, 7]:
if cell == player['location']:
print(tile.format('X'), end='')
elif cell in player['path']:
print(tile.format('.'), end='')
else:
print(tile.format('_'), end='')
else:
if cell == player:
print(tile.format('X|'))
elif cell in player['path']:
print(tile.format('.|'))
else:
print(tile.format('_|'))
monster, door, player['location'] = get_locations()
logging.info('monster: {}; door: {}; player: {}'.format(monster, door, player['location']))
while True:
moves = get_moves(player['location'])
print("Welcome to the dungeon!")
print("You're currently in room {}".format(player['location']))
draw_map()
print("\nYou can move {}".format(', '.join(moves)))
print("Enter QUIT to quit")
move = input("> ")
move = move.upper()
if move == 'QUIT':
break
if not move in moves:
print("\n** Walls are hard! Stop running into them! **\n")
continue
player = move_player(player, move)
if player['location'] == door:
print("\n** You escaped! **\n")
break
elif player['location'] == monster:
print("\n** You got eaten! **\n")
break
else:
continue
What's confusing is that draw_map() definitely draws 'X's when the player's location is the same as the cell, and we get the player's location right before the while loop, so when we draw map, it should be there. Kevin mentions in his video that he doesn't want the X there though.
Further, when I just ran this program a few times, the first time the X was there, the second time it was not, and it seems to be there about half of the time? Some console output below:
Welcome to the dungeon!
You're currently in room (1, 2)
||||
||||
|||_|
You can move LEFT, UP, DOWN
Enter QUIT to quit
Welcome to the dungeon!
You're currently in room (2, 2)
||||
||||
|||_|
You can move LEFT, UP
Enter QUIT to quit
Welcome to the dungeon!
You're currently in room (2, 0)
||||
||||
|X|||
You can move RIGHT, UP
Enter QUIT to quit
This is all really new and confusing to me, so thanks so much for the help!
All the best, Scott
Steven Parker
243,656 PointsSteven Parker
243,656 PointsThere are no dumb questions. But it would be sure helpful to anyone wanting to give assistance if you would include a link to the course video that you are referring to. And show any code you've written (if that's related to the issue).