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 trialKelllen Esquibel
Courses Plus Student 1,219 PointsIt appears to be right to me. Testing just fine in IDLE. Please help.
It appear to be right to me and tests just fine in IDLE.
Please Help
# 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 += direction[0]
y += direction[1]
boardRange = list(range(0, 10))
if x or y not in boardRange:
hp -= 5
if x not in boardRange:
x -= direction[0]
if y not in boardRange:
y -= direction[1]
return x, y, hp
Kelllen Esquibel
Courses Plus Student 1,219 PointsThe code above wasn't passing the challenge.
What I was trying to accomplish was only deducting the 'hp' by 5 one time in the case that 'x' and 'y' both go out of range on the same move. For example, if player position is (x, y, hp = (0, 9, 10)), and the direction(-1, 1), then both 'x' and 'y' would be out of range but hp only being deducted by 5 once to '5' rather than twice to '0'.
Do you think my code is the best way to accomplish this?
Kellen
2 Answers
Alexander Davison
65,469 PointsHere's your code:
def move(player, direction):
x, y, hp = player
x += direction[0]
y += direction[1]
boardRange = list(range(0, 10))
if x or y not in boardRange:
hp -= 5
if x not in boardRange:
x -= direction[0]
if y not in boardRange:
y -= direction[1]
return x, y, hp
The condition x or y not in boardRange
may not do what you think it does. Try x not in boardRange or y not in boardRange
instead. Or, you could do this: (In my opinion, this is a bit cleaner.)
def move(player, direction):
x, y, hp = player
x += direction[0]
y += direction[1]
boardRange = list(range(0, 10))
if x not in boardRange:
x -= direction[0]
hp -= 5
if y not in boardRange:
y -= direction[1]
hp -= 5
return x, y, hp
I hope this helps! ~Alex
Kelllen Esquibel
Courses Plus Student 1,219 PointsThank you for the response Alex.
What I was trying to accomplish was only deducting the 'hp' by 5 one time in the case that 'x' and 'y' both go out of range on the same move. For example, if player position is (x, y, hp = (0, 9, 10)), and the direction(-1, 1), then both 'x' and 'y' would be out of range but hp only being deducted by 5 once to '5' rather than twice to '0'.
Do you think my code is the best way to accomplish this?
I had looked at other responses to this same challenge and I mistakenly thought this was a requirement. I never even tried running without this perceived requirement, and turns out, your code is correct. Thank you for your help!
Kellen
Josh Keenan
20,315 PointsJosh Keenan
20,315 PointsWhat is the problem?