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

Please help me understand why my tuple changes in the Python Basics Dungeon Game?

I'm in the middle of trying to program the dungeon game. tryna do it myself first without looking at the videos. please try not to give hints/corrections regarding the script in general if possible, b/c i'd like to figure it out myself. But i had a question regarding how my tuple is being changed? I will explain more by showing my print outputs. below is my in-progress script.

#Create a game with a 2-dimensional map. Place the player, a door, and a monster into random spots in your map. Let the player move around in the map and, after each move, tell them if they've found the door or the monster. If they find either the game is over. The door is the win condition, the monster is the lose condition.

import random

def show_help():
    print("Welcome to the Dungeon. Try to reach the door w/o running into the monster.")
    print("Enter 'UP', 'DOWN', 'LEFT', or 'RIGHT' to move around the map. \nEnter 'HELP' for this info")

def direction(inp, li):
    if inp == 'HELP':
        show_help()
    elif inp == 'UP':
        li[1] = li[1]-1
    elif inp == 'RIGHT':
        li[0] = li[0]+1
    elif inp == 'DOWN':
        li[1] = li[1]+1
    elif inp == 'LEFT':
        li[0] = li[0]-1
    else:
        print('NOT VALID')
    return li

def wall(li):
    if li[1] < 1:
        li[1] = li[1]+1
    elif li[0] > 6:
        li[0] = li[0]-1
    elif li[1] > 6:
        li[0] = li[0]-1
    elif li[0] < 1:
        li[1] = li[1]+1


columns = list(range(1,7))
rows = list(range(1,7))

gridlist = []
for column in columns:
    for row in rows:
        gridlist.append([column, row])
grid = tuple(gridlist)
print("gridin: {}".format(grid))

player = random.choice(gridlist)
gridlist.remove(player)  

door = random.choice(gridlist)
gridlist.remove(door)

monster = random.choice(gridlist)

show_help()

while player != door:
    if player == door:
        print("Congrats! You made it out alive!")
        break
    elif player == monster:
        print("You just got eaten!")
        break
    elif not player in grid:
        print("There's a wall here. Can't go this direction further")
        #player = wall(player)
        break
    else:
        print("playerin: {}".format(player))
        way = input("> ")
        player = direction(way, player)
        print("playerafter: {}".format(player))
        print("grid: {}".format(grid))
        print("gridlist: {}".format(gridlist))
        continue

I happened upon this because I was printing things at different locations to find out why my script was infinite looping/notworking. So when I first run my script, I have it print out my grid, a tuple and where my player initially is, a list.

treehouse:~/workspace$ python dungeon_game.py
gridin: ([1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6])
Welcome to the Dungeon. Try to reach the door w/o running into the monster.
Enter 'UP', 'DOWN', 'LEFT', or 'RIGHT' to move around the map.
Enter 'HELP' for this info
playerin: [1, 6]

Then I input a command and look at this craziness!

UP
playerafter: [1, 5]
grid: ([1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 5], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4 ], [6, 5], [6, 6])
gridlist: [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [ 6, 6]]
playerin: [1, 5]

The point on the grid where the player is at the beginning of the game gets changed to wherever he moves next. I thought tuples were immutable? Hope that makes sense. I'd appreciate any insight.

2 Answers

If a tuple references a mutable object, such as a list, then its value can be changed.

Here's an excellent post about tuples: Python tuples: immutable but potentially changing

So, technically the tuple is still pointing to/has the value of the same object, the list, which has changed.

thanks iain, i just read that article the other day when i was googling.

If my response answered your question, or at least would make things clearer for others viewing this forum post, please mark it as 'Best Answer'. :)