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

can someone help me out here please

How am I to do this challenge and what is wrong with my code?

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
    if move == 'LEFT':
        x -= 1
        if x == 0:
            hp -= 5
    if move == 'RIGHT':
        x += 1
        if x == 9:
            hp -= 5
    if move == 'UP':
        y -= 1
        if y == 0:
            hp -= 5
    if move == 'DOWN':
        y += 1
        if  == 9:
            hp -= 5
    return x, y, hp

3 Answers

Hi Ian,

The instructions say Don't let them go past the wall. So, you need to catch that scenario. Your code allows the move first then deducts hp if x == 0. If the player is already at x == 0, then your code allows a move to -1. Maybe check to see if x == 0 before the move is made. If x == 0 deduct 5 hp points but don't move the position. A player can arrive at x == 0 without penalty; it is moving beyond zero or 9 that's the issue.

Make sense?

Steve.

Hi Ian,

Sorry - I only just read the challenge properly - I need more coffee!!

The direction the player moves in is sent into the method as a parameter. The method receives two parameters, a player which contains a position and an hp level, and a direction, which is a tuple of the x & y changes to make to the player position already passed in.

You've got the player position in the three local variables, x, y and hp. You then want to do the test as in my first answer but use the direction tuple to make those changes.

Does that make sense? There's no string being sent in like 'LEFT' or 'RIGHT'; just a pair of numbers which change the values of x & y inside player, unless the position is already next to the wall.

Let me know how you get on.

Steve.

Yep - I got this working.

I unwrapped (whatever the correct term is) the tuple passed in called direction and placed its two values in two more local variables. I called them xd and yd to represent the change in value of x and y being requested.

I then tested if the existing position x plus the requested change xd was either greater than 9 or less than 0. If that's the case, I deducted 5 from hp; otherwise, I allowed the addition to take place as the move is legal.

That all passed the challenge.