Reeborg's World Around 1 - Variable (Python's While Loop)
I'm New to Programming and I'm Trying My Hand at Reeborg's World Problems. However, I've Reached a Block at Around 1 - Variable. This Is My Code Right Now: Def...
I'm new to programming and I'm trying my hand at Reeborg's World problems. However, I've reached a block at Around 1 - Variable.
This is my code right now:
def walk():
if wall_on_right()==True and wall_in_front()==False:
move()
if wall_in_front()==True:
turn_left()
move()
put("token")
move()
while object_here("token")!=True:
walk()
if object_here("token")==True:
done()
The problem is that it creates an infinite loop. It seems that the while function is not recognizing the token put at the starting position as a the condition that breaks out of the loop.
Thank you for your help.
3 Answers
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
move()
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
def turn_up():
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
#for steps in range (0, 5):
#jump()
#hurdle = 6
while at_goal() != True:
if wall_in_front() == True:
turn_up()
elif front_is_clear() == True:
move()
This is how I solved mine for puzzle 3
This is how I finished the same exercise:
put()
while not wall_in_front():
move()
if wall_in_front():
turn_left()
elif object_here():
done()
Basically:
- It moves straight while there is not wall in the front and it keeps checks with a while loop if there is wall or not in front.
- If there is a wall it turns left and gets in while loop again.
- To stop the while loop; to determine the starting point first I left a token with
put()function. With anelifstatement afterifstatement I have usedobject_here()function so it can detect the object and stop the while loop withdone()function.
If anyone has an better solution for that I'd love to see different approaches.
I fiddled a little more on this problem and found a way to solve it
def walk():
while wall_on_right() and not wall_in_front():
if object_here():
take()
done()
else:
move()
def turn():
while wall_on_right() and wall_in_front():
turn_left()
move()
put("token")
move()
while True:
walk()
turn()
The problem mostly consists of how I was understanding the object_here ()
function since, as mentioned in their page "Reeborg can determine whether or not there is at least one object at his location". Therefore, adding a take () and
done () function in case Reeborg detected an object in a given field was able to do the trick.