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 (2016, retired 2019) Dungeon Game Movement

David P
PLUS
David P
Courses Plus Student 1,162 Points

my code print("Enter QUIT to quit") is not working. It says invalid syntax. What is the correct syntax?

my code print("Enter QUIT to quit") is not working. It says invalid syntax. What is the correct syntax?

Anna Tvelova
Anna Tvelova
4,060 Points

same issue at the same spot :(

File "game.py", line 53
print("Enter QUIT to quit")
^
SyntaxError: invalid syntax

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya David! There is nothing wrong with your "print" statement but did you capture the user's input, like how else you gonna use the input "QUIT" to your advantage and do stuff with. "input" method does just that. You can print a statement and capture the user's input with 'input()' method and store it in a variable and use it for your coding logic later.

P.S. : You might wanna copy your code here, so that we can get an idea about where the issue actually resides.

user_input = input(" What's your name? ")

This is merely an example! I hope it helped!(:

David P
David P
Courses Plus Student 1,162 Points

mport os import random

draw grid

pick random location for player

pick random loocation for exit door

pick random location for the monster

draw player in the grid

take input for movement

move player, unless invalid move (past edges of grid)

check for win/loss

clear screen and redraw grid

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]

def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear')

def get_locations(): return random.sample(CELLS, 3)

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

def get_moves(player): moves = ["LEFT", "RIGHT", "UP", "DOWN"] x, y = player if x == 0: moves.remove("LEFT") if x == 4: moves.remove("RIGHT") if y == 0: moves.remove("UP") if y == 4: moves.remove("DOWN") # if players y == 0, they cant move up # if players y == 4, they cant move down return moves

monster, door, player = get_locations()

while True: print("Welcome to the dungeon!") print("You're currently in room {}".format(player)) print("You can move {}".format(", ".join(get_moves(player)))) print("Enter QUIT to quit")

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

if move == 'QUIT': break

Good move? Change the player position # Bad Move? Don't change anything! # On the door? They win! # on the monster? They lose! # Otherwise, loop back around

sorry here is my full code ^^^...... for some reason i am getting an error with print("Enter QUIT to quit"), but Kenneth has the same syntax from what I can see in the educational video.

David P
PLUS
David P
Courses Plus Student 1,162 Points

import os import random

draw grid

pick random location for player

pick random loocation for exit door

pick random location for the monster

draw player in the grid

take input for movement

move player, unless invalid move (past edges of grid)

check for win/loss

clear screen and redraw grid

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]

def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear')

def get_locations(): return random.sample(CELLS, 3)

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

def get_moves(player): moves = ["LEFT", "RIGHT", "UP", "DOWN"] x, y = player if x == 0: moves.remove("LEFT") if x == 4: moves.remove("RIGHT") if y == 0: moves.remove("UP") if y == 4: moves.remove("DOWN") # if players y == 0, they cant move up # if players y == 4, they cant move down return moves

monster, door, player = get_locations()

while True: print("Welcome to the dungeon!") print("You're currently in room {}".format(player)) print("You can move {}".format(", ".join(get_moves(player)))) print("Enter QUIT to quit")

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

if move == 'QUIT': break

# Good move? Change the player position # Bad Move? Don't change anything! # On the door? They win! # on the monster? They lose! # Otherwise, loop back around

sorry here is my full code ^^^...... for some reason i am getting an error with print("Enter QUIT to quit"), but Kenneth has the same syntax from what I can see in the educational video.