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

I can't figure out why my code isn't working. It returns "player" just fine but the function isn't changing the values.

Someone else in Community already asked my first question, which was about how to change the tuple for "player," and the answer was to change the values of x, y, and hp rather than change the tuple itself. This is what I was trying to do with my if/else chunks but they are obviously not working because the values for "player" when it's returned are the same as the input.

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

1 Answer

Steven Parker
Steven Parker
243,656 Points

Your function doesn't directly change player

But you could return the values you did change instead:

    return (x, y, hp)

At some point in constructing that code I had it that way and it wasn't working. I obviously changed it to player and changed something else in the interim. I have also since changed my 2nd-4th "if" blocks to "elif" -- I think that's the only other thing? (Adding this for anyone else who winds up with this problem.) Thanks.