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 trialRujuta Shinde
1,712 Pointsthis code is working correctly but throwing error here
def move(player, direction):
x, y, hp = player
x1, y1 = direction
if (x == 0 or x==9) and (y!=0 and y!=9) :
hp= hp-5
y = y+y1
elif (y==0 or y==9) and (x!=0 and x!=9):
hp = hp-5
x = x+x1
elif (y==0 or y==9) and (x==0 or x==9):
hp = hp -5
else:
x= x+x1
y=y+y1
return x, y, hp
# 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
x1, y1 = direction
if (x == 0 or x==9) and (y!=0 and y!=9) :
hp= hp-5
y = y+y1
elif (y==0 or y==9) and (x!=0 and x!=9):
hp = hp-5
x = x+x1
elif (y==0 or y==9) and (x==0 or x==9):
hp = hp -5
else:
x= x+x1
y=y+y1
return x, y, hp
1 Answer
Steven Tagawa
Python Development Techdegree Graduate 14,438 PointsI think the biggest problem is that your code isn't testing the right values. The challenge is to test to see if the new coordinates would be out of bounds, but your code is testing x and y, which are the original coordinates. And since the original coordinates are always going to be valid, your if expressions are never going to work out right. To see if the new coordinates are out of bounds, you would need to test (x+x1) and (y+y1) instead of x and y.
(Also, 0 and 9 are in bounds, so x or y being exactly 0 or 9 shouldn't subtract from hp. Only values below 0 or above 9 should reduce the hp variable. So (x+x1)<0 would probably work better than x == 0. It would also catch numbers that are even further out of bounds, like -1, -2, etc.)