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

Raduica Sebastian
PLUS
Raduica Sebastian
Courses Plus Student 1,356 Points

What is wrong with this code?

I can't understand why it doesn't work

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
    dx,dy = direction
    if x == 0:
        if dx == -1:
            hp-=5
            continue
    if x == 9:
        if dx == 1:
            hp-=5
            continue
    if y == 0:
        if dy == -1:
            hp-=5
            continue
    if y == 9:
        if dy == 1:
            hp-=5
            continue

    return x, y, hp

1 Answer

Gabbie Metheny
Gabbie Metheny
33,778 Points

Right now, your code is only addressing the hit points (hp), but you also need to deal with moving the player. If the moves would take the player outside the grid, you only want to reduce the hit points, but if they don't, you need to move the player to the new position before returning x, y and hp. You could accomplish this by using if, elif and else, although all your current tests could be simplified into a single if using the or keyword to test for multiple conditions:

if x + dx < 0 or this or this or that:
    # do something with hp
else:
    #do something with x, y
return x, y, hp

Let me know how it goes!