Why does my Turtle code not work when wrapped in a function?

Viewed 30

I was going through a exercise with Turtle that requested me to create a random walk with Turtle and the exercise was easy enough but since I'm trying to get in the habit of creating functions for my code, I decided to wrap the small chunk of code within a new function like so:

from turtle import *
from random import randint, choice

random_direction = [
    0,
    90,
    180,
    270
]

colormode(255)
width(15)
speed("fastest")


def random_walk():
    for n in range(200):
        color(
            randint(0, 255),
            randint(0, 255),
            randint(0, 255))
        forward(30)
        setheading(choice(random_direction))


exitonclick()

random_walk()

This makes the GUI pop up but nothing else. And if I just run this without calling it through a function, it runs perfect.

What am I not understanding about the python logic here? I feel like this has worked before for me outside of Turtle use.

0 Answers
Related