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 trialOllie Webster
5,035 PointsI am not getting the right movement results!
Apparently this is not passing because I am not getting the right movement results. The hp decrement works fine when the player crashes, and the movement results seem fine in a separate workspaces window. Any suggestions?
The only thing I can think of is if it wants me to move the x or y even if only the other one would cause a crash. i.e. trying to move in direction (-1, 1) from position (0, 0). Should this not move at all or just permit the y move to 1?
# 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
x_move, y_move = direction
if not (0 <= x + x_move <= 9) or (0 <= y + y_move <= 9):
hp = hp - 5
else:
x = x + x_move
y = y + y_move
return x, y, hp
1 Answer
Steven Parker
231,269 PointsIn programming, comparisons must be done individually and combined with logical operators, so where an expression like this might make sense in mathematics:
(0 <= x + x_move <= 9)
the program equivalent would be:
(0 <= x + x_move and x + x_move <= 9)
Also, it might help make things less complicated and reduce the chance for errors if you restate the comparisons so you don't need to use "not". For example: "not a <= b" is the same thing as just "a > b"