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 trialIKE EGBuns
3,848 Pointsmovement.py, my code is not working and needs refactoring. Don't really know where I am getting it wrong.
I have just checked my code and it seems not to work when i submitted it for checks, but it seem to run on my IDE. I wouldn't know where I am getting it wrong. Can i also get help with refactoring the code. Thanks for any help in advance.
# 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 make_wall():
made_wall = []
for y in range(10):
for x in range(10):
made_wall.append((x, y))
return made_wall
wall = make_wall()
hp = 10
x, y = random.choice(wall)
player = x, y , hp
def move(player, direction):
x, y , hp = player
i, r = direction
task = ''
while task != "T":
task = input("type t to quit ")
task = task.upper()
if task == "T":
break
if x < 0 or y < 0:
hp = hp - 1
x = x + i
y = y + r
else:
x = x + i
y = y + r
return x, y, hp
2 Answers
James Shi
8,942 PointsFirst, you only need to submit the function move, so all of the code above it should be removed. The function shouldn't take in user input, so you don't need the task variable or any loop. Your condition for checking out of bounds only checks if the coordinates before moving are outside two of the walls. You want to check if the coordinates after moving are outside all four of the walls. The hp should decrease by 5, not 1, if the player runs into a wall, and the coordinates of the player shouldn't change in that case.
IKE EGBuns
3,848 PointsI am really Thankful, James. Worked like a breeze. U are a gem. thanks buddy