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

My Dungeon game code isn't working any help would be appreciated.

When I make it to the door I get this error:

UnboundLocalError: local variable 'output' referenced before assignment.

This is my code:

import os
import random
import pep8
# draw grid
# random location for player
# random location for exit door
# random location for monster  <<<<<maybe make random type of monster>>>>>
# draw player
# take input for movement
# move player, unless invalid move
# check for win/loss
# clear screen and redraw board


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

cells1 =  [(0,0), (1,0), (2,0), (3,0), (4,0), (5,0), 
           (0,1), (1,1), (2,1), (3,1), (4,1), (5,1),
           (0,2), (1,2), (2,2), (3,2), (4,2), (5,2),
           (0,3), (1,3), (2,3), (3,3), (4,3), (5,3), 
           (0,4), (1,4), (2,4), (3,4), (4,4), (5,4),
           (0,5), (1,5), (2,5), (3,5), (4,5), (5,5),
           (0,6), (1,6), (2,6), (3,6), (4,6), (5,6),
           (0,7), (1,7), (2,7), (3,7), (4,7), (5,7), 
           (0,8), (1,8), (2,8), (3,8), (4,8), (5,8),
           (0,9), (1,9), (2,9), (3,9), (4,9), (5,9),
           (0,10), (1,10), (2,10), (3,10), (4,10), (5,10),
           (0,11), (1,11), (2,11), (3,11), (4,11), (5,11),
           (0,12), (1,12), (2,12), (3,12), (4,12), (5,12),
           (0,13), (1,13), (2,13), (3,13), (4,13), (5,13),
           (0,14), (1,14), (2,14), (3,14), (4,14), (5,14),
           (0,15), (1,15), (2,15), (3,15), (4,15), (5,15),
           (0,16), (1,16), (2,16), (3,16), (4,16), (5,16),
           (0,17), (1,17), (2,17), (3,17), (4,17), (5,17),
           (0,18), (1,18), (2,18), (3,18), (4,18), (5,18)]


def locations_producer():
    return random.sample(cells1, 8)

#this is for catty-corner movement
def move_player(player, move):
    x,y = player
    if move == "A":
        x -= 1

    if move == "D":
        x += 1

    if move == "W":
        y -= 1

    if move == "S":
        y += 1

    if move == "WA":
        x -= 1
        y -= 1

    if move == "WD":
        x += 1
        y -= 1

    if move == "SA":
        y += 1
        x -= 1

    if move == "SD":
        y += 1
        x += 1

    return x, y




def get_moves(player):

    moves = ["A", "D", "W", "S","WD","WA","SD","SA"]

    x,y = player
    if x == 0:
        moves.remove("A")
        moves.remove("WA")
        moves.remove("SA")

    if x == 5:
        moves.remove("D")
        moves.remove("WD")
        moves.remove("SD")

    if y == 0:     
        moves.remove("W")
        moves.remove("WD")
        moves.remove("WA")

    if y == 18:    
        moves.remove("S")
        moves.remove("SD")
        moves.remove("SA")

    return moves

def draw_map(player, person, door, monster1, monster2, monster3, monster4, monster5, monster6):
#    , monster1, monster2, monster3, monster4, monster5, monster6
    print("___"*6)
    tile = "|{}"

    for cell in cells1:
        x,y = cell
        if x < 5:
            line_end = ""
            if cell == player:
                output = tile.format(person)
            elif cell == door:
                output = tile.format("[]")

#This is for after winning or losing all six monsters show     
            elif person == ":(" or person == ":D":
                if cell == monster1:
                    output = tile.format("};")

                elif cell == monster2:
                    output = tile.format("};")

                elif cell == monster3:
                    output = tile.format("};")

                elif cell == monster4:
                    output = tile.format("};")

                elif cell == monster5:
                    output = tile.format("};")

                elif cell == monster6:
                    output = tile.format("};")

            else:
                output = tile.format("__")
        else:
            line_end = ">\n"
            if cell == player:
                output = tile.format("{}|".format(person))

            elif cell == door:
                output = tile.format("[]|")

            elif person == ":(" or person == ":D":    
                if cell == monster1:
                    output = tile.format("};|")

                elif cell == monster2:
                    output = tile.format("};|")

                elif cell == monster3:
                    output = tile.format("};|")

                elif cell == monster4:
                    output = tile.format("};|")

                elif cell == monster5:
                    output = tile.format("};|")

                elif cell == monster6:
                    output = tile.format("};|")

            else:
                output = tile.format("__|")
        print(output, end = line_end)

def game_loop():
    monster1, monster2, monster3, monster4, monster5, monster6, door, player = locations_producer()

#    print(monster, door, player)

    valid_moves = get_moves(player)
    print("Hello escaper!")
    input("Press enter to start")
    person = ":)"
    clear_screen()

    while True:
        valid_moves = get_moves(player)

        draw_map(player, person, door, monster1, monster2, monster3, monster4, monster5, monster6)
#    , monster1, monster2, monster3, monster4, monster5, monster6
#        print("You are in room {}.".format(player))
        if player == door:
            clear_screen()
            person = ":D"
            draw_map(player, person, door, monster1, monster2, monster3, monster4, monster5, monster6)
#    , monster1, monster2, monster3, monster4, monster5, monster6            
            end_of_game = input(" You found the door!!! You win!!! Do you want to play again? :) Y/n ")
            if end_of_game.upper() == "N":
                print("Goodbye")
                break
            else:
                clear_screen()
                print("OK! Let's go!")
                game_loop()
                break
        elif player == monster1 or player == monster2 or player == monster3 or player == monster4 or player == monster5 or player == monster6:
            clear_screen()
            person = ":("
            draw_map(player, person, door, monster1, monster2, monster3, monster4, monster5, monster6)

            end_of_game = input("Oh no!!! You were eaten by the monster! Do you want to play again? :( Y/n ")

            if end_of_game.upper() == "N":
                clear_screen()
                print("Goodbye")
                break
            else:
                clear_screen()
                print("OK! Let's go!")
                game_loop()
                break

        else:

            print("You can move {} directions.".format(", ".join(valid_moves)))   
            print("Enter QUIT to quit ")

            print("I am windows streets and trips glad to help an escaping spy.")

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

            if move == 'QUIT':
                clear_screen()
                print("Goodbye")
                break


        #    else:
        #        player = move_player(player, move)

            if move in valid_moves:
                player = move_player(player, move)
                person = ":)"
            else:
                print("\n ** Walls are hard! Don't run into them! :| ** \n")
                person = ":|"
#                continue

game_loop()

#checker = pep8.Checker('breakout.py')
#checker.check_all()    

Thank you whoever answers. :)

3 Answers

Hey, pet this just means that you need to actually create a variable before doing anything with it. In this case the simplest solution is just to set output = None before your for loop in the draw_map() function. I tested it afterwards and it worked perfectly for me.

def draw_map(player, person, door, monster1, monster2, monster3, monster4, monster5, monster6):
    print("___"*6)
    tile = "|{}"

    # This is where I'd put it
    output = None

    for cell in cells1:
        x,y = cell
        if x < 5:
            line_end = ""
            if cell == player:
                output = tile.format(person)
            elif cell == door:
                output = tile.format("[]")

Thank you.

Happy to help!

Now I found another problem diogorferreira. When I reach a corner it tells me:
moves.remove("WD"or"WA"or"SD"or"SA")
ValueError: list.remove(x): x not in list
Thanks in advance. :)