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 trialDmitriy Ignatiev
Courses Plus Student 6,236 PointsMovement.py
Hi, there.
Could anybody clarify why my code doesn't work but it works in shell.
Thank you
# 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 x == 0:
hp -= 5
if x == -1:
hp -= 10
if y == 0:
hp -= 5
if y == -1:
hp == 10
return x, y, hp
2 Answers
Kenneth Love
Treehouse Guest TeacherYou're not accounting for the direction the player is trying to travel. Just because the player is at x=0
doesn't mean they lose 5 HP. They'd only lose that if they were trying to travel to x=-1
. Same for other cases where they're trying to leave the map.
Dennis Zheleznyak
4,155 PointsHi there,
This is my solution that worked:
def move(player, direction):
x, y, hp = player
xd, yd = direction
if x+xd < 0 or x+xd > 9:
hp -= 5
pass
else:
x += xd
if y+yd < 0 or y+yd > 9:
hp -= 5
pass
else:
y += yd
return x, y, hp
Stanisław Dudzik
9,731 PointsI know it's been a long time since your post. Can you explain why are you using "pass" in your conditions?