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

The 'player' variable: what is being stored in it and where is it being assigned that?

This is the code I have so far. Can someone explain where the 'player' variable is being assigned something and what it's being assigned.

Thanks!

import os
import random

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, 3), (3, 2), (4, 2),
         (0, 3), (1, 3), (2, 4), (3, 3), (4, 3),
         (0, 4), (1, 4), (2, 5), (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):
    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")

    return moves


monster, door, player = get_locations()
while True:
    print("Welcome to the dungeon!")
    print("You're currently in room {}".format(player))  # fill with player position
    print("You can move {}".format(", ".join(get_moves(player))))  # fill with availible moves
    print("Enter QUIT to quit")

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

    if move == 'QUIT':
        break

1 Answer

Steven Parker
Steven Parker
243,656 Points

The "player" value is assigned with a 2-element tuple on this line:

monster, door, player = get_locations()

The function "get_locations" returns a list of 3 randomly-selected cells and "player" is assigned the third value.

Thank you.