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

Jonathan Walker
Jonathan Walker
4,240 Points

Hit Points Challenge

So I run this code in workspaces and it works just fine. Does everything asked as far as I can tell. Not sure why the challenge doesn't like it?

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
    i, r = direction

    if x!=0 and x!=9 and y!=0 and y!=9:
        x += i
        y += r
    else:     
        if x == 0:
            if i == 1:
                x += 1
            if i == -1:
                hp -= 5
                x = 0
        if x == 9:
            if i == 1:
                hp -= 5
            if i == -1:
                x -= 1

        if y == 0:
            if r == 1:
                y += 1
            if r == -1:
                hp -= 5
        if y == 9:
            if r == 1:
                hp -= 5
            if r == -1:
                y -= 1


    return x, y, hp


move((0, 2, 10), (-1, -1))
Jonathan Walker
Jonathan Walker
4,240 Points

"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."

That's the prompt for the challenge. Thanks for taking a look!

3 Answers

Right now, your code doesn't address the fact that you should still move in left or right even if you can't move up or down. Similarly, your code doesn't allow you to move up or down if you don't move left or right. Right now, the code only allows you to move at all if you aren't on a wall. You can fix this by changing around the order of some of your if-else statements.

Steven Parker
Steven Parker
231,275 Points

You can simplify this code a great deal if you want.

I agree with Travis, but I would also point out that if you revise your initial if conditions to check if the intended move is allowed, then the else only needs to do one thing: reduce the hp.

So the whole function would have just one if, and one else. Very "DRY". :smile:

Jonathan Walker
Jonathan Walker
4,240 Points

Thanks a bunch! been out of town, but sitting down now to give what you guys said a try.