Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dungeon Game Hit points

tyler bucasas
tyler bucasas
2,453 Points

movement.py

someone 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
    if direction == (1,0):
        x += 1
        elif x == 9:
            hp -= 5
    if direction == (-1,0):
        x -= 1
        elif x == 0:
            hp -+ 5
    if direction == (0,1):
        y  +=1
        elif y == 9:
            hp -+ 5
    if direction == (0,-1):
        y -= 1
        elif y == 0:
            hp -= 5

return x, y, hp

2 Answers

Steven Parker
Steven Parker
216,045 Points

A few hints:

  • an "elif" will always line up (start in the same column) as the corresponding "if"
  • on the other hand, a nested "if" will be indented (but just spelled "if")
  • the symbols "-+" written together are not a valid Python operator
  • 0 and 9 are both allowed values within the limits
  • if the movement goes outside the limits, the original x and y should be returned unchanged
  • movement might occur in both directions at the same time
  • movement might be for more than one unit in one or both directions ‡

‡ = I'm not sure if the validator tests that last one, but the instructions don't eliminate the possibility.

tyler bucasas
tyler bucasas
2,453 Points
def move(player, direction):
    x, y, hp = player

    if direction == (1,0) and x <= 9:
        x += 1
    else:
        hp -= 5

    if direction == (-1,0) and x >= 0:
        x -= 1
    else:
        hp -= 5

    if direction == (0,1) and y <= 9:
        y  += 1
    else:
        hp -= 5

    if direction == (0,-1) and y >= 0:
        y -= 1
    else:
        hp -= 5

    return x, y, hp

Steven Parker pls help

Steven Parker
Steven Parker
216,045 Points

You still have to handle the possibility that the movement may be in both dimensions at the same time. And you might need to account for more than one unit of motion as well.

You might want to split up "direction" and check each dimension separately.