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 trialAnibal Marquina
9,523 PointsI need a hand in this challenge
The code in working in local python shell.. but not to overcome the challenge!!
Our game's player only has two attributes, x and y coordinates. Let's practice with a slightly different one, though. This one has x, y, and "hp", which stands for hit points. Our move function takes this three-part tuple player and a direction tuple that's two parts, the x to move and the y (like (-1, 0) would move to the left but not up or down). Finish the function so that if the player is being run into a wall, their hp is reduced by 5. Don't let them go past the wall. Consider the grid to be 0-9 in both directions. Don't worry about keeping their hp above 0 either.
# 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
if x < 0:
hp -= 5
x += 1
elif direction == (1,0):
x += 1
if x > 9:
hp -= 5
x -= 1
elif direction == (0,-1):
y -= 1
if y < 0:
hp -= 5
y += 1
elif direction == (0,1):
y += 1
if y > 9:
hp -= 5
y -=1
return x, y, hp
14 Answers
Alexander Davison
65,469 PointsDude, your code passes the challenge.
Try refreshing the page or something
Congrats on finishing the challenge
I hope this helps. ~Alex
Chris Grazioli
31,225 PointsKenneth Love I knew it had to be something stupid I wasn't focusing on...
def move(player, direction):
x, y, hp = player
xx, yy = direction #corrected that "XX" error
if xx == -1 and x==0:
hp-=5
elif xx == 1 and x==9:
hp-=5
elif yy ==-1 and y==0:
hp-=5
elif yy == 1 and y==9:
hp-=5
else:
x+=xx
y+=yy
return x, y, hp
Martynas Zilinskas
22,087 PointsInteresting how you concentrated on hp rather on position, what amazes me, that you can achieve the same thing different way :)
def move(player, direction):
x, y, hp = player
xx, yy = direction
if 9 > x > 0 and xx == -1:
x -= 1
elif 9 > x > 0 and xx == 1:
x += 1
elif 9 > y > 0 and yy == -1:
y -= 1
elif 9 > y > 0 and yy == 1:
y += 1
else:
hp -= 5
return x, y, hp
Vasilieios Papapostolou
3,413 PointsJust a simple solution to help you all :)
def move(player, direction):
x, y, hp = player
x1, y1 = direction
if (x1+x==-1) or (x1+x==10) or (y1+y==-1) or (y1+y==10):
hp -= 5
else:
x += x1
y += y1
return x, y, hp
Heather Malloch
2,141 PointsCan you explain how you got to this please? Thanks btw!
Brady Huang
16,360 PointsMy answer here, which go well: def move(player, direction): x, y, hp = player direction_x, direction_y = direction
if (9 >= x+direction_x >= 0) & (9 >= y+direction_y >= 0):
x += direction_x
y += direction_y
else:
hp -= 5
return x, y, hp
Gerard Nwazk
Courses Plus Student 2,833 Pointswooow @ Brady...
if (9 >= x+direction_x >= 0) & (9 >= y+direction_y >= 0):
But Thats new... can you throw more knowledge on that? thanks in advance
George Lewiss
1,219 PointsYour code doesn't work when I run it. The logic makes sense, but it isn't working. It says it isn't getting the right movement result.
Zachary Claire
11,259 PointsMe and Brady did something very similar, I just used a lot of and's whereas his solution looks quite a bit more mathematical (and elegant). I learned something new. :)
def move(player, direction):
x, y, hp = player
movX, movY = direction
if (x + movX >= 0 and x + movX <= 9) and (y + movY >= 0 and y + movY <= 9):
x += movX
y += movY
else:
hp -= 5
return x, y, hp
Chris Grazioli
31,225 PointsI went a different way with my code than the solution listed above. Its should do the same thing though. what am I missing here?
``python
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
XX, yy = direction
if xx == -1 and x==0:
hp-=5
x=0
elif xx == 1 and x==9:
hp-=5
x=9
elif yy == -1 and y==0:
hp-=5
y=0
elif yy == 1 and y==9:
hp-=5
y=9
return x, y, hp
Alexander Davison
65,469 PointsThere's always more then one way to solve a problem :)
Chris Grazioli
31,225 Points@Kenneth Love I don't understand the point of adjusting the player position x-=1, just to adjust it back x+=1 when you check to verify the direction hasn't positioned the player beyond a wall. Especially considering that initial movement doesn't really get returned back. My code looks to see if the player is currently at a wall AND has a direction that will send him passed the wall, then says okay and docks the points but leaves them in the position they started in and returns that. Plus it unloads the direction tuple like we've been taught these last few videos. I'm looking at this code that passes and comparing what mine is supposed to be doing... and banging my head
I need fresh eyes or someone to tell me what my codes is doing wrong
def move(player, direction):
x, y, hp = player
XX, yy = direction
if xx == -1 and x==0: # so if your direction is left and your position is at the wall
hp-=5 # loose 5 hitpoints
x=0 # reset positon back to where you where
elif xx == 1 and x==9:
hp-=5
x=9
elif yy == -1 and y==0:
hp-=5
y=0
elif yy == 1 and y==9:
hp-=5
y=9
return x, y, hp
Kenneth Love
Treehouse Guest TeacherYou created XX
on line 2 of the function, but you're using xx
throughout the function. Those aren't the same variable.
When I run your code, I get an error about move
not being found, which is likely 'caused by that variable naming error (since the function has invalid code, it's not available to be used, so it can't be found).
Chris Grazioli
31,225 Pointsdef move(player, direction):
x, y, hp = player
xx, yy = direction #corrected that "XX" error
if xx == -1 and x==0:
hp-=5
x=0
elif xx == 1 and x==9:
hp-=5
x=9
elif yy ==-1 and y==0:
hp-=5
y=0
elif yy == 1 and y==9:
hp-=5
y=9
return x, y, hp
Kenneth Love I fixed that ridiculous error you found for me (amazing what a fresh pair of eyes can see), I don't even know how I did that? But I'm still getting a BUMMER back, "Hmm, didn't get the right movement results."
all I'm doing is checking to see if the player is at the wall, docking points and resetting x or y. Now that I think about it, I shouldn't even have to change x or y, just leave them the same and return them
def move(player, direction):
x, y, hp = player
xx, yy = direction #corrected that "XX" error
if xx == -1 and x==0:
hp-=5
elif xx == 1 and x==9:
hp-=5
elif yy ==-1 and y==0:
hp-=5
elif yy == 1 and y==9:
hp-=5
return x, y, hp
What else do you think could be keeping this from working???
Kenneth Love
Treehouse Guest TeacherYou're never actually moving the player, you're only focusing on the hp
. Nothing in the instructions tell you to not move them if the move is valid. If they're at, say, x=5
and the move is (-1, 0)
, they should end up at x=4
.
Nebiyou Abebe
3,914 Pointsdef move(player, direction):
x, y, hp = player
a, b = direction
x += a
y += b
if x < 0:
hp -= 5
x = 0
elif y < 0:
hp -= 5
y = 0
elif x > 0:
hp -= 5
x = 9
elif y > 9:
hp -= 5
y = 9
return x, y, hp
Nebiyou Abebe
3,914 PointsCan anyone help me with this. It worked for all the examples, but I when I run it, it says the middle one is not right.
Kenneth Love
Treehouse Guest TeacherWhy are you always removing 5 hp and setting the x
to either 0 or 9 (same on y
)? It's perfectly possible for a player to be somewhere that isn't an edge. In that case, they should just be moved to their new location and their health shouldn't be affected.
Nebiyou Abebe
3,914 PointsOh I now know the problem, it was supposed to be X > 9 not not X > 0.
Carlos Marin
8,009 PointsInteresting how we all have different solutions. Here is my solution:
def move(player, direction):
x, y, hp = player
direction_x, direction_y = direction
if x in range(0, 9):
x += direction_x
if y in range(0, 9):
y += direction_y
else:
hp -= 5
return x, y, hp
Xiang Liu
24,339 PointsIt make sense. Thanks Carios.
Anibal Marquina
9,523 Pointsthx, dont get why always that happends
Alexander Davison
65,469 PointsNo problem :)
Can you please provide a Best Answer so that this question is marked as answered? Thank you!
~Alex
Chris Grazioli
31,225 PointsAlexander Davison True, but in my case I can't see why its not passing the grader. It should do the same thing, but something is jamming me up right now
Nebiyou Abebe
3,914 PointsI removed 5 hp and set x to either 0 or 9 only when the move leads to off the edge (which is when x ,< 0 or X > 9). Is it not right?