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

Gilbert Craig
Gilbert Craig
2,102 Points

Are my 2 while loops incorrect ?

In my code I allow x and y to be incremented unless it would put the, outside the grid. If they hit the wall in either direction they lose 5 points and the direction co-ordinate is not incremented with the direction value ...

Still getting bummer .. Any help appreciated

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
    xdir, ydir = direction
    while x + xdir <=9 or >= 0:
        while y + ydir <=9 or >= 0:
            y = y + ydir
            else:
                hp -= 5
        x = x + xdir
    else:
        hp -= 5

    return x, y, hp

1 Answer

Dear Gilbert,

first of all in the while statement is a syntax error while x + xdir <=9 or >= 0: => while x + xdir <=9 or x + xdir >= 0:

But why anyway a while? Over what you wanna iterate?

Let me say it in words

  1. you wanna check if x+xdir is lower than 9 AND x+dir is higher than 0
  2. you wann chekc if y+ydir is lowet than 9 AND y+dir is higher than 0

otherwise it gonna hurts. I hope you can solve it with this hint.

Greetings Julian

Gilbert Craig
Gilbert Craig
2,102 Points

thanks Julian, yes it did hurt ! lol i realised i had created an infinite loop monster .. If statements resolved this, thanks.