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

Robert Baucus
Robert Baucus
3,400 Points

Dungeon Game : get_locations(), "monster not defined" argh!

import random
import os
import sys

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():
    if os.name == 'nt':
        os.system('cls')
    else: os.system('clear')

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


def move_player(player, move):
    #ascertain current player 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_move(player):
    move = ["LEFT", "RIGHT", "UP", "DOWN"]
    x, y = player
    if x == 0:
        move.remove("LEFT")
    if x == 4:
        move.remove("RIGHT")
    if y == 0:
        move.remove("UP")
    if y == 4:
        move.remove("DOWN")
    return move



monster, door, player  = get_locations()

while True:
    print("Welcome to the DUNGEON !")
    print("You are currently in room {}".format(player)) 
    print("You can move {}".format(", ".join(get_move(player))))
    print("Enter QUIT to quit")

    move = input("> ")
    move = move.upper()
    if move == 'QUIT':
        print("I guess you were afraid of the DUNGEON!")
        break
    When I put this into python I get the follow error from the shell:

Traceback (most recent call last): File "C:\Program Files\Python36\learningPython\dungeon_game.py", line 44, in <module> monster, door, player = get_locations() File "C:\Program Files\Python36\learningPython\dungeon_game.py", line 18, in get_locations return monster, door, player NameError: name 'monster' is not defined

For the life of me I can't figure out what is wrong ? Could someone please point me in the right direction

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You do not have to name the return values, Since random.sample returns a list, it will be unpacked at the calling statement. Instead, simply use:

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

For more on random.sample see [docs].

Robert Baucus
Robert Baucus
3,400 Points

Wow. I spent so much time thinking it was a syntax error. Thanks Chris!