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

It appears to be right to me. Testing just fine in IDLE. Please help.

It appear to be right to me and tests just fine in IDLE.
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 += direction[0]
    y += direction[1]
    boardRange = list(range(0, 10))
    if x or y not in boardRange: 
        hp -= 5
    if x not in boardRange: 
        x -= direction[0]
    if y not in boardRange: 
        y -= direction[1]
    return x, y, hp

The code above wasn't passing the challenge.

What I was trying to accomplish was only deducting the 'hp' by 5 one time in the case that 'x' and 'y' both go out of range on the same move. For example, if player position is (x, y, hp = (0, 9, 10)), and the direction(-1, 1), then both 'x' and 'y' would be out of range but hp only being deducted by 5 once to '5' rather than twice to '0'.

Do you think my code is the best way to accomplish this?

Kellen

2 Answers

Here's your code:

def move(player, direction):
    x, y, hp = player 
    x += direction[0]
    y += direction[1]
    boardRange = list(range(0, 10))
    if x or y not in boardRange: 
        hp -= 5
    if x not in boardRange: 
        x -= direction[0]
    if y not in boardRange: 
        y -= direction[1]
    return x, y, hp

The condition x or y not in boardRange may not do what you think it does. Try x not in boardRange or y not in boardRange instead. Or, you could do this: (In my opinion, this is a bit cleaner.)

def move(player, direction):
    x, y, hp = player 
    x += direction[0]
    y += direction[1]
    boardRange = list(range(0, 10))
    if x not in boardRange: 
        x -= direction[0]
        hp -= 5
    if y not in boardRange: 
        y -= direction[1]
        hp -= 5
    return x, y, hp

I hope this helps! :grin: :zap: ~Alex

Thank you for the response Alex.

What I was trying to accomplish was only deducting the 'hp' by 5 one time in the case that 'x' and 'y' both go out of range on the same move. For example, if player position is (x, y, hp = (0, 9, 10)), and the direction(-1, 1), then both 'x' and 'y' would be out of range but hp only being deducted by 5 once to '5' rather than twice to '0'.

Do you think my code is the best way to accomplish this?

I had looked at other responses to this same challenge and I mistakenly thought this was a requirement. I never even tried running without this perceived requirement, and turns out, your code is correct. Thank you for your help!

Kellen