I just started learning Python from interactivepython.org and am stuck with the below exercise:
Modify the turtle walk program so that you have two turtles each with a random starting location. Keep the turtles moving until one of them leaves the screen.
This is what I came up with. The program stops with 1 iteration and both turtles start from the same coordinates for some reason.
import turtle
import random
wn = turtle.Screen()
kj = turtle.Turtle()
saklep = turtle.Turtle()
saklep.color('green')
c = random.randrange(1,100)
v = random.randrange(1,100)
b = random.randrange(1,100)
n = random.randrange(1,100)
kj.setx(c)
kj.sety(v)
saklep.setx(b)
saklep.sety(n)
def isinscreen(t,s,w):
leftmost=-(w.window_width())/2
rightmost =(w.window_width())/2
uppermost = (w.window_height())/2
bottommost =-(w.window_height())/2
tx = t.xcor()
ty = t.ycor()
sx = s.xcor()
sy = s.ycor()
if tx > rightmost or ty >uppermost:
return False
elif tx < leftmost or ty < bottommost:
return False
elif sx > rightmost or sy > uppermost:
return False
elif sx < leftmost or sy < bottommost:
return False
else:
return True
while isinscreen(kj,saklep,wn) == True:
for i in random.randrange(1,361):
kj.forward(100)
kj.left(i)
for deg in random.randrange(1,361):
saklep.forward(100)
saklep.right(deg)
wn.exitonclick()