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 2

SyntaxError with (tile.format('_'), end=' ') in Python 3.5.0

Hi guys,

When I try to run the code in the workspace console, I keep getting an SyntaxError specifically pointing to the '=' in (tile.format('_'), end=' ')

I looked around the community and there was a similar problem, but it was related to Python 2.

Any help?

Steven Parker
Steven Parker
218,814 Points

That snippet is a bit to small to analyse. Could you post the whole code?
Or even better, make a snapshot of your workspace and post the link to it.

2 Answers

Julien riera
Julien riera
14,665 Points

Haven't you forget "print" there ?

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

Also, you may not wanna put those spaces which will break your squares.

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 loc.
    monster = random.choice(CELLS)

    # door = random loc.
    door = random.choice(CELLS)

    # player start = random loc.
    player = random.choice(CELLS)

    # if monster, door, and player start are the same, do it again
    if monster == door or monster == player or door == player:
        return get_locations()

    # return monster, door, player
    return monster, door, player


def move_player(player, move):
    # player = (x, y)
    x, y = player
    # get the player's current location
    # if move is LEFT, x - 1
    if move == 'LEFT':
        x -= 1
    # if move is RIGHT, x + 1
    elif move == 'RIGHT':
        x += 1
    # if move is UP, y - 1
    elif move == 'UP':
        y -= 1
    # if move is DOWN, y + 1
    elif move == 'DOWN':
        y += 1

    return x, y

def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
    # player = (x, y)

    # if player x is 0, remove LEFT
    # if player x is 2, remove RIGHT
    if player[0] == 0:
        moves.remove('LEFT')
    if player[0] == 2:
        moves.remove('RIGHT')

    # if player y is 0, remove UP
    # if player y is 2, remove DOWN
    if player[1] == 0:
        moves.remove('UP')
    if player[1] == 2:
        moves.remove('DOWN')

    return moves

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

    for idx, cell in enumerate(CELLS):
        if idx in [0, 1, 3, 4, 6, 7]:
            if cell == player:
                print(tile.format('X'), end=' ')
            else:
                (tile.format('_'), end=' ')
        else:
            if cell == player:
                print(tile.format('X|'))
            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))

    draw_map(player)

    print("You can move {}.".format(moves))
    print("Enter QUIT to quit.")

    move = input("What's your move? ")
    move = move.upper()

    if move == 'QUIT':
        break

    # if it's a good move, change the player's position
    if move in moves:
        player = move_player(player, move)
    else:
        print("Walls are hard, stop walking into them.")
        continue
    # if it's a bad move, don't change anything
    # if the new player position is the door, they win
    if player == door:
        print("You've escaped!")
        break
    # if the new player position is the monster, they lose
    elif player == monster:
        print("You were eaten by the monster!")
        break
    # otherwise, continue