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

tomasmotycka
tomasmotycka
7,158 Points

Why this code isnΒ΄t passing the challenge while it works in workspace?

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.

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
    a, b = direction
    x += a
    y += b
    if x + a < 0:
        x = 0
        hp -= 5
    if x + a > 9:
        x = 9
        hp-=5
    if y + b < 0:
        y = 0
        hp -= 5
    if y + b > 9:
        y = 9
        hp-=5

    return x, y, hp
Chris Sehnert
Chris Sehnert
30,857 Points

It seems to me that case #1 is returning (0, 1, 5) when it should return (0, 1, 10).... ....hmmmm.....

i think you are performing the addition operation more often than you need to.... ...ie. you give x and y their new values......and then add them to a/b again in your if blocks.....

hope this helps....I'll stay tuned....

-chris

1 Answer

tomasmotycka
tomasmotycka
7,158 Points

Than you Chris, now I see it. There should be ony x or y compared with 0 in each "if" condition. Now it passes.

Chris Sehnert
Chris Sehnert
30,857 Points

excellent!.....it took me a couple of tries as well.....thanks for helping me brush up on my Python syntax....it had been a while ...I've been learning some other languages.....

Happy Coding!