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 Python Collections (Retired) Dungeon Game Building the Game: Part 2

Michael Randall
PLUS
Michael Randall
Courses Plus Student 10,643 Points

Error with (tile.format("x"), end = ' ') in python 3.4.2

I keep getting an "invalid syntax" error message when I try to run my code: Specifically, the error message points to the equal (=) sign, directly behind the first "end". While I started with my own version, I basically had to backtrack and copy the exact code from the demonstration, but it still doesn't seem to work.

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:
                print(tile.format('_'), end = " ")
        else:
            if cell == player:
                print(tile.format('X|'))
            else:
                print(tile.format('_|'))

3 Answers

Michael Randall
PLUS
Michael Randall
Courses Plus Student 10,643 Points

Ok, the simple fix is for me to use python3 from the terminal. Apparently python 2.7.5 doesn't have the " end="" " " syntax capability.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Weird. I don't see anything syntactically wrong and my local terminal is fine with it. It also runs fine locally.

Can you copy and paste the entire error?

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

File "./Dungeons.py", line 70 print(tile.format("X"), end = " ") ^ SyntaxError: invalid syntax

I've tried single and double quotes. I've also played around with the spacing. Something similar works fine in the terminal, but when I try to run it in the script it generates that error. I've looked online for the syntax for print and the format method, and everything seems fine. But still doesn't work. Thanks for your help. Also, I enjoy your classes.


I couldn't find the place to directly respond to your last comment so I've added it in two places:

By shebang, I'm using #! /usr/bin/python. I'm using the IDLE shell editor and it says I'm using Python 3.4.2.

Here is my entire code:

! /usr/bin/python

import random

a list of tuples

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) # start is also known as player

#clever - if any match, recall the function, if no match, return takes you out
if monster == door or monster == start or door == start:
    return get_locations()

return monster, door, start

def move_player(player, move): # player = (x,y) 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']

if player[1] == 0:
    moves.remove('LEFT')
if player[1] == 2:
    moves.remove('RIGHT')
if player[0] == 0:
    moves.remove('UP')
if player[0] == 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:
            print(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 are currently in room {}".format(player)) # fill in with player/start position
draw_map(player)
print("You can move {}".format(moves)) # fill in with available moves
print("Enter QUIT to quit")

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

if move == 'QUIT':
    break

if move in moves:
    player = move_player(player, move)
else:
    print("Walls are hard, watch out!")
    continue

if player == door:
    print("You escaped")
    break
elif player == monster:
    print("You were eaten")
    break
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, there is nothing wrong with that code, syntax-wise, unless you're actually running it under Python 2.

You're running the file as an executable. What shebang did you put into it?

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

By shebang, I'm using #! /usr/bin/python. I'm using the IDLE shell editor and it says I'm using Python 3.4.2.

Here is my entire code:

#! /usr/bin/python

import random

#a list of tuples
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) # start is also known as player

    #clever - if any match, recall the function, if no match, return takes you out
    if monster == door or monster == start or door == start:
        return get_locations()

    return monster, door, start


def move_player(player, move):
    # player = (x,y)
    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']


    if player[1] == 0:
        moves.remove('LEFT')
    if player[1] == 2:
        moves.remove('RIGHT')
    if player[0] == 0:
        moves.remove('UP')
    if player[0] == 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:
                print(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 are currently in room {}".format(player)) # fill in with player/start position
    draw_map(player)
    print("You can move {}".format(moves)) # fill in with available moves
    print("Enter QUIT to quit")

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

    if move == 'QUIT':
        break

    if move in moves:
        player = move_player(player, move)
    else:
        print("Walls are hard, watch out!")
        continue

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


print get_locations()
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

If that last line is running with no complaints, you're using Python 2 instead of Python 3.

Add this in another file in IDLE and run it just like you've been running this one:

import sys
print(sys.version)
Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

Running ''' import sys print(sys.version) '''

Returns: ''' 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] '''

Alexander Torres
Alexander Torres
4,486 Points

@Michael Randall

have you tried to import "from future import print_function" at the beginning of the file? Worked for me when you are using python version 2.x