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 trialWes House
6,944 PointsWhy return x,y and not return player?
For the code below, the instructor deletes "return player" and replaces it with "return x,y" and the only explanation as to why is because he wants the values. But why can't it be "return player" since it's already set to x,y? Was that just an arbitrary decision?
def move_player(player, move): x,y = player
if move == "LEFT":
x -= 1
if move == "RIGHT":
x += 1
if move == "UP":
y -= 1
if move == "DOWN":
y += 1
return x,y
1 Answer
Steven Parker
231,236 PointsThe code changes x and y based on the direction of the move. If you were to return "player", you're just giving back the same values you started with. By returning "x, y" you are giving back the new values after the move.
Pitrov Secondary
5,121 PointsPitrov Secondary
5,121 PointsAnd because the player is a tuple and you can't change tuples? I am not sure, am I right?
Steven Parker
231,236 PointsSteven Parker
231,236 PointsYou can't modify the tuple, but you could always assign "player" with a new one. But that's not necessary since you can just return a new one directly.
Jake Simpson
4,550 PointsJake Simpson
4,550 PointsYour answer makes sense, but I'm still a bit confused. I understand that just returning "player" in this function would give you back the original values, but how does the code know that "x, y" are the new values for player?
Steven Parker
231,236 PointsSteven Parker
231,236 PointsIt's not the function itself that associates the return with "player", but the code where the function is called:
player = move_player(player, move)
The calling code uses the tuple that the function returns and assigns the "player" variable with it.