As you can see from my code, I've written a simple program with the turtle module to randomly draw lines with a random color. The turtle will randomly move forward or backward with a random distance which is chosen from a list. What I would like to do is to prevent the turtle to go outside the window border. I've tried different things, searched on this website, also asked to chatGpt but none solution found solved the problem. Can someone fix my code explaining to me what I'm doing wrong? import random import turtle from turtle import Turtle, Screen my_screen = Screen() my_screen.setup(800, 600) my_turtle = Turtle() my_turtle.speed(2) my_turtle.shape("turtle") my_turtle.pensize(5) turtle.colormode(cmode=255) #turtle_screen_size = turtle.screensize() def random_color(): r_color = random.randint(1,255) g_color = random.randint(1,255) b_color = random.randint(1,255) random_color = (r_color,g_color,b_color) return random_color distance = [50,60,70,80,90,100] turns = ["left", "right"] directions = ["forward", "backward"] for _ in range(100): my_turtle.pencolor(random_color()) my_turtle_turns = random.choice(turns) my_turtle_direction = random.choice(directions) if my_turtle_turns == "left": my_turtle.left(90) else: my_turtle.right(90) if my_turtle_direction == "forward": my_turtle_distance = random.choice(distance) my_turtle.forward(my_turtle_distance) else: my_turtle_distance = random.choice(distance) my_turtle.backward(my_turtle_distance) for _ in range(100): my_turtle.pencolor(random_color()) my_screen.exitonclick() The turtle keeps going outside the boundaries. Continue reading...