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

What's wrong with my code?

I have spent the better part of an hour looking this over and I can't figure out what is wrong with it. Help!

def move(player, direction):
    x, y, hp = player
    moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    if x == 0:
        moves.remove((1, 0))
    if x == 9:
        moves.remove((-1, 0))
    if y == 0:
        moves.remove((0, -1))
    if y == 9:
        moves.remove((0, 1))
    if direction not in moves:
        hp -= 5
    else:
        if direction == (1, 0):
            x -= 1
        if direction == (-1, 0):
            x += 1
        if direction == (0, 1):
            y -= 1
        if direction == (0, -1):
            y += 1
    return x, y, hp

Hi Nichole,

I'm not sure if this is for a code challenge or not but videos, quizzes, and code challenges all have a "Get Help" button on them. If you ask your question through that interface then it will be automatically linked to that piece of content.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Guessing at your coordinates, if (0,0) is upper left and (9,9) is lower right, then it appears your else direction` changes for y are reversed. That is, for a direction change of (0, 1) it should move the y column to the right, so (0, 0) + (0, 1) => (0, 1). The code returns a negative:

# testing in the REPL
>>> def move(player, direction):
...     x, y, hp = player
...     moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
...     if x == 0:
...         moves.remove((1, 0))
...     if x == 9:
...         moves.remove((-1, 0))
...     if y == 0:
...         moves.remove((0, -1))
...     if y == 9:
...         moves.remove((0, 1))
...     if direction not in moves:
...         hp -= 5
...     else:
...         if direction == (1, 0):
...             x -= 1
...         if direction == (-1, 0):
...             x += 1
...         if direction == (0, 1):
...             y -= 1
...         if direction == (0, -1):
...             y += 1
...     return x, y, hp
...
>>> move((0,0,15), (1,0))  # can't move "up", loss of 5 hp
(0, 0, 10)
>>> move((0,0,15), (-1,0))  # can move "down" X goes 0 -> 1
(1, 0, 15)
>>> move((0,0,15), (0,1))  # can't move "up" but does so anyway. Error
(0, -1, 15)
>>> move((0,0,15), (0,-1))  # can move down, but gets hp loss instead. Error
(0, 0, 10)

I believe the fix is in exchanging the y -= 1 and the y += 1 statements

Chris, I made the changes and it still didn't pass. I took a few days off to clear my head and came back to it. Still can't figure it out.

def move(player, direction):
    x, y, hp = player
    moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    if x == 0:
        moves.remove((1, 0))
    if x == 9:
        moves.remove((-1, 0))
    if y == 0:
        moves.remove((0, -1))
    if y == 9:
        moves.remove((0, 1))
    if direction not in moves:
        hp -= 5
    else:
        if direction == (-1, 0):
            x -= 1
        if direction == (1, 0):
            x += 1
        if direction == (0, -1):
            y -= 1
        if direction == (0, 1):
            y += 1
    return x, y, hp

I did other variations, like this plus switching the y's. Nothing seems to be working.

Dammit! I NOW works. Why does it do that? Kept saying Bummer! etc etc... Sorry for the trouble but thank you.