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!

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 Write Better Python Buggy Logs Logging

Anthony Grodowski
Anthony Grodowski
4,902 Points

game.log not working

In the python shell after typing ls game.log doesn't show up. I get only smth like this:

treehouse:~/workspace$ ls badpep8.py buggy.py dd_game.py docstrings.py logging.py __pycache__


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)]:
    if player in [(0, 0), (0, 1), (0, 2)]:
    if player in [(0, 2), (1, 2), (2, 2)]:
    if player in [(2, 0), (2, 1), (2, 2)]:
    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='')
                print(tile.format('_'), end='')
            if cell == player['location']:
            elif cell in player['path']:

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']))


    print("\nYou can move {}".format(', '.join(moves)))
    print("Enter QUIT to quit")

    move = input("> ")
    move = move.upper()

    if move == 'QUIT':

    if not move in moves:
        print("\n** Walls are hard! Stop running into them! **\n")

    player = move_player(player, move)
    if player['location'] == door:
        print("\n** You escaped! **\n")
    elif player['location'] == monster:
        print("\n** You got eaten! **\n")
jmac pd
jmac pd
11,490 Points

I ran this fine. when I copied and saved the code and ran the program through the command line. must be something with the workspace. Maybe try copying it to your python IDE shell or anther text editor, then re-aligning the spaces and tabs in the setting, then pasting it back to the workspace.

Short of that I am not sure, but it is saving the log every time I move.

1 Answer

So I copied your code and I was able to create and log out to game.log.

treehouse:~/workspace$ ls                                                                                                      
badpep8.py  buggy.py  dd_game.py  docstrings.py  game.log  game.py                                                             
treehouse:~/workspace$ cat game.log                                                                                            
INFO:root:monster: (0, 0); door: (1, 2); player: (1, 0)                                                                        
treehouse:~/workspace$ python --version                                                                                        
Python 3.6.4                                                                                                                   

can you type

python game.py

then quit out and then do an

ls -al
14,241 Points

Hi there josh, what is this -al command?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

The -al are switches to the shell command ls.

  • -a says display all files, including "dot" files
  • -l says display long listing which includes permissions and file size