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

movement.py works in the console but not in the website

my code works in console but not in the website i don't know why?

console code with output

>>> def move(player, direction):
...     x, y, hp = player
...     dirx , diry = direction
...     if x + dirx < 0:
...         hp-=5
...         x = 0
...     if x + dirx > 9:
...         hp-=5
...         x = 9
...     if y + diry > 9:
...         hp-=5
...         y = 9
...     if y +diry < 0:
...         hp-=5
...         y = 0
...     return x, y, hp
...
>>> move((0, 9, 5), (0, 1))
(0, 9, 0)
>>> move((0, 1, 10), (-1, 0))
(0, 1, 5)
>>>

website 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
    dirx , diry = direction
    if x + dirx < 0:
        hp-=5
        x = 0
    if x + dirx > 9:
        hp-=5
        x = 9
    if y + diry > 9:
        hp-=5
        y = 9
    if y +diry < 0:
        hp-=5
        y = 0
    return x, y, hp

1 Answer

Elad Ohana
Elad Ohana
24,456 Points

Hi Suheyb,

I tried the code at the bottom on my computer and it seems to work fine for what it's supposed to do. In your second workspace, you quoted 3 examples. The second and third work as you set up your code. The first one does not because you have not set up the code to do anything unless the results are past the limits (less than 0, greater than 9). There is nothing in your code to set up what to do in between. Since your first example has (x, y) at (1, 1) with a direction of (-1, 0). None of the if statements will execute: since 1 minus 1 = 0, x is not < 0 or > 9. Maybe this is something you were planning to set up at a later stage, but I think your code is working for what you set it up to do. If I misunderstood which part is not working, please let me know, as I did not see it specified.

Best, Elad