Python Python Collections (2016, retired 2019) Dungeon Game Hit points

Daniel Adari
Daniel Adari
1,261 Points

Help in Hit Points challenge!

I have tried solving it in this way with no success... I know there is an easier way but I want to complete it this way... Please help :(

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
    x_move, y_move = direction
    nx = x + x_move
    ny = y + y_move

    if (nx) in range(0, 9):
        x = nx
    elif nx > 9:
        hp -= 5
        x = 9
    else:
        x = 0

    if (ny) in range(0, 9):
        y = ny
    elif ny > 9
        hp -= 5
        y = 9
    else:
        y = 0

    return x, y, hp

3 Answers

Oszkár Fehér
Oszkár Fehér
Treehouse Project Reviewer

Hi Daniel Adari. Actually, you are closer than you may think :smile: , your approach it's good and it works just fine, double check the second elif statement, something it's missing from the end. Happy coding.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi Daniel,

I had another look at this one to remind myself how this challenge worked.

I did the same as you and created two more variables to represent the x and y change. I then tested whether x plus the change in x position was either less than zero or greater than 9. If so, do nothing to x but take 5 off hp otherwise, allow the change in the else part of the conditional test. Then do the same with y.

I hope that makes sense.

Steve.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi Daniel,

You need to test for scenarios where x or y may become less than zero; not just greater than 9.

I hope that helps a little! Else, get back to me and I'll explain further ... post any new code, though.

Steve.