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

Kars Jansens
Kars Jansens
4,774 Points

I'm not sure why this doesn't work {python}

question: Our game's player only has two attributes, x and y coordinates. Let's practice with a slightly different one, though. This one has x, y, and "hp", which stands for hit points.

Our move function takes this three-part tuple player and a direction tuple that's two parts, the x to move and the y (like (-1, 0) would move to the left but not up or down).

Finish the function so that if the player is being run into a wall, their hp is reduced by 5. Don't let them go past the wall. Consider the grid to be 0-9 in both directions. Don't worry about keeping their hp above 0 either.

Can someone help me pls?

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
    mx, my = direction
    x += mx
    y += my
    if x > 9:
        hp -= 5
        x =  9
    if x < 0:
        hp -= 5
        x = 0
    if y < 0:
        hp -= 5
        x = 0
    if y > 9:
        hp -=5
        x = 9
    return x, y, hp
Kars Jansens
Kars Jansens
4,774 Points

it now works in workspace but it still says it's wrong. It says the hp is wrong.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 57,146 Points

You are very close! There appear to be typos in the code. Hint: Where are the reassignments for y when it is out of bounds?

Post back if you need more help. Good luck!

Kars Jansens
Kars Jansens
4,774 Points

Thank you, I have another question maybe you also could help me with that one. https://teamtreehouse.com/community/i-dont-know-why-this-isnt-working-python