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 1

Charles Harpke
Charles Harpke
33,986 Points

Building the Game: Part 1

There is something syntactically wrong with the code. I get this error when running-- Traceback (most recent call last): File "/Users/charpke/Desktop/dungeon_game.py", line 61, in <module> move = input("> ") File "<string>", line 1, in <module> NameError: name 'LEFT' is not defined. Here is my code:

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.choice(CELLS)
    door = random.choice(CELLS)
    start = random.choice(CELLS)

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

    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

monster, door, player = get_locations()

while True:
    moves = get_moves(player)

    print("Welcome to the dungeon!")
    print("You're currently in room {}".format(player))
    print("You can move {}".format(moves)) 
    print("Enter QUIT to quit")

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

    if move == 'QUIT':
        break

    if move in moves:
        player = move_player(player, move)
    else:
        print("** Walls are hard, stop walking into them! **")
        continue

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

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Are you using Python 2 locally instead of Python 3? If so, you'll need to use raw_input() instead of input(). (or, better yet, upgrade to Python 3).

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Charles.

When does this error appear? Because I have launched the file both locally and on treehouse workspaces and it did not return any error.

Charles Harpke
Charles Harpke
33,986 Points

I have Python3 inatalled....

charpke$ python3
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  5 2014, 20:42:22) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Charles Harpke
Charles Harpke
33,986 Points

Here is what I did, as I am not that familiar with the Terminal:

cd Desktop to view my desktop applications. dungeon_game.py appears there. Then run python3 dungeon_game.py and it launches successfully. to the local.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

If you check out the Setting Up a Local Python Environment course, there's a bit of Teacher's Notes about changing your shell's PATH.