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

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Don't really know if I understood the instructions correctly

Here's what I tried. It looks OK to me but maybe I did not understand the instructions correctly.

BR, Kimmo

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, Y = direction

    while True
        if x+X < 0:
            hp = hp-5
            break
        elif x+X > 9:
            hp = hp-5
            break
        elif y+Y < 0:
            hp = hp-5
            break
        elif y+Y > 9:
            hp = hp-5
            break

    x = x + X
    y = y + Y      

    if x > 9:
        x = 9
    elif x < 0:
        x = 0
    if y > 9:
        y = 9
    elif y < 0:
        y = 0 

    return x, y, hp

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

There are two issues with using the while loop.

  • A missing colon after while True is a syntax error
  • if the new position doesn't go past a wall, then the while loops forever!

You are actually very close. The while loop can be replace with the one of the following:

# use just the if portion
    #while True:
    if x+X < 0:
            hp = hp-5
            # break
    elif x+X > 9:
            hp = hp-5
            #break
    elif y+Y < 0:
            hp = hp-5
            #break
    elif y+Y > 9:
            hp = hp-5
            #break

# or combine the if/elif into a single if:
    if x+X < 0 or x+X > 9 or y+Y < 0 or y+Y > 9:
        hp = hp-5
Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Thanks Chris! I really appreciate your way of pointing out the things that need to be fixed. It was easy to fix the code after reading your comments.

BR, Kimmo