The Python Oracle

Randomly walking turtle function not doing what I want it to

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Isolated

--

Chapters
00:00 Randomly Walking Turtle Function Not Doing What I Want It To
01:23 Accepted Answer Score 3
02:14 Answer 2 Score 0
02:46 Answer 3 Score 2
03:12 Thank you

--

Full question
https://stackoverflow.com/questions/1806...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #turtlegraphics

#avk47



ACCEPTED ANSWER

Score 3


Your counter = counter + 1 is wrong. When your isInScreen returns False, the while loop breaks and the code ends, since, the counter is being incremented but you don't loop over again. See the following code -

import turtle
import random

def isInScreen(w,t):
    leftBound = w.window_width() / -2.0
    rightBound = w.window_width() / 2.0
    bottomBound = w.window_height() / -2.0
    topBound = w.window_height() / 2.0

    turtlex = t.xcor()
    turtley = t.ycor()

    if turtlex < leftBound or turtlex > rightBound or turtley < bottomBound or turtley > topBound:
        return False

    return True

def randomWalk(t,w):
    counter = 0

    while True:
        while isInScreen(w,t):
            coin = random.randrange(0,2)
            if coin == 0:
                t.left(90)
            else:
                t.right(90)
            t.forward(50)
        t.left(180)
        t.forward(50)
        counter += 1
        if counter == 4:
            break

wn = turtle.Screen()
wn.bgcolor('lightcyan')

steklovata = turtle.Turtle()
steklovata.color('darkslategray')
steklovata.shape('turtle')

randomWalk(steklovata,wn)

wn.exitonclick()

P.S - You don't need a variable to store stillIn, if the if condition evaluates to True, just return False, and if it doesn't return True. (Changes reflected in the above code).




ANSWER 2

Score 2


As an alternative to the nested loops and to avoid some redundant statements, the following should produce the same result as Sukrit's answer.

def randomWalk(t,w):
    counter = 0

    while counter < 4:
        if not isInScreen(w,t):
            t.left(180)
            counter += 1
        else:
            coin = random.randrange(0,2)
            if coin == 0:
                t.left(90)
            else:
                t.right(90)
        t.forward(50)

The core issue is making sure that isInScreen returning false does not cause your while loop to terminate while also incrementing counter within the loop body.




ANSWER 3

Score 0


Your big while loop there is the cause of failure and you know that. So what you can do is make the while loop just check the counter only and not do the isInScreen() as part of the while loop. Now for checking if you can go forward, you can cheat by looking before you leap, that is add the value of fifty to your current position and check to see if you will be in screen, if not go forward, otherwise go as close as you can, increment the collision counter, and then turn around. Alternatively, Sukrit Kalra's answer might be easier to implement.