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 (2016, retired 2019) Dungeon Game Hit points

Ronald Tse
Ronald Tse
5,798 Points

How can we change tuples??

both player and direction are tuples, but then I'm required to change the player tuple... HOW?

movement.py
# EXAMPLES:
# move((1, 1, 10), (-1, 0)) => (0, 1, 10)
# move((0, 1, 10), (-1, 0)) => (0, 1, 5)
# move((0, 9, 5), (0, 1)) => (0, 9, 0)

def move(player, direction):
    x, y, hp = player
    if direction = (-1, 0):
        if player[0] = 0:
            player[2] - 5
        else:
            player[0] - 1
    if direction = (1, 0):
        if player[0] = 9:
            player[2] - 5
        else:
            player[0] + 1
    if direction = (0, -1):
        if player[1] = 0:
            player[2] - 5
        else:
            player[1] - 1
    if direction = (0, 1):
        if player[1] = 9:
            player[2] - 5
        else:
            player[1] + 1
    return x, y, hp

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

yes, player parameter is tuple, (tuple is immutable, you got that right). But you aren't supposed to modify the player tuple.

x, y, hp = player

Because the setup in this line, values in the player tuple's were decomposed into three variables, x,y,hp, if change needs to be made, make the change to these 3 variables.

Then at the very last line of the code return x, y, hp, a new tuple is returned.

PS: I'm not sure if this was covered during the lecture, but return x, y, hp is equivalent to return (x, y, hp).