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 trialIan Maina
4,571 PointsWhat is wrong with this code
help me out here
# 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
xd, yd = direction
if xd + x >= 9:
hp -= 5
else:
x += xd
if xd - x <= 0:
hp -= 5
else:
x -= xd
if yd - y <= 0:
hp -= 5
else:
y -= yd
if yd + y >= 9:
hp -= 5
else:
y += yd
return x, y, hp
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYou are headed in the right direction, but have some issues:
"...if the player is being run into a wall, their hp is reduced by 5. Don't let them go past the wall."
if the position would be greater than 9 or less than 0, set the value to the max 9 or min 0 instead.
You are using <= 0
and >= 9
when it should be just '< 0' and '> 9'.
The x += xd
statements may be executed twice for positions not near the borders. Same for y += xd
.
The two if
statements for x and for y checking should use if... elif... else
format.
The position change may be positive or negative relative value
always use plus to add the new direction to the current position. You are mixing pluses and minuses.
Post back if you need more help. Good luck!!