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 trialSahar Nasiri
7,454 PointsIt is not working!
What is wrong with my code that makes it not work?
# 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 & (direction[0] < 0):
x, y, hp = x, direction[1] + y, hp - 5
if x == 9 & (direction[0] > 0):
x, y, hp = x, direction[1] + y, hp - 5
if y == 0 & (direction[1] < 0):
x, y, hp = direction[0] + x, y, hp - 5
if y == 9 & (direction[1] > 0):
x, y, hp = direction[0] + x, y, hp - 5
else:
x, y, hp = direction[0] + x, direction[1] + y, hp
return x, y, hp
2 Answers
gyorgyandorka
13,811 PointsThe ampersand (&
) is the so-called "bitwise" AND operator in Python, it can only be used on integer values by default, and works differently than the conditional AND (what we usually need), for which the keyword is and
.
Christian Mangeng
15,970 PointsHi Sahar,
Gyorgy is right, you need to use "and" instead of "&". Also, why do you want to change the position of the player if the player performs an invalid move? The only thing that should happen in that case is that hp is reduced by 5.
One useful advice here: it is a good idea to unpack the direction variable in this case for reasons of readability (you assign direction[0] to "a" and direction[1] to "b"):
a, b = direction
Good luck
Sahar Nasiri
7,454 PointsThanks for your great advice :)