0

I am trying to run the following program:

import turtle

def draw_square():
    window = turtle.Screen()
window.bgcolor("red")

brad = turtle.Turtle()
#count=0
# while(count<4):
brad.forward(100)
brad.right(90)
#    count = count + 1
wait_for_user()    
window.exitonclick()
brad.done()

draw_square

But when I run it, nothing happens. I don't see any output as such. I get blank message in console too.

1 Answer 1

1

When you define a function like draw_square, anything you make inside of that function stays inside of it. In this case, you say

def draw_square():
    window = turtle.Screen()

That's fine, but outside of the function, there is no such thing as window. So you should then get an error when you try window.bgcolor("red").

You have two choices: (1) delete that function line and unindent the definition of window; (2) indent everything after the definition, so that it is also inside the function, then call the function with draw_square() after you've defined it.

Another problem: wait_for_user() is not defined. Is this a method of brad, or window, or a function inside turtle?

This works for me:

import turtle

def draw_square():
    window = turtle.Screen()
    window.bgcolor("red")
    brad = turtle.Turtle()
    brad.forward(100)
    brad.right(90)
    window.exitonclick()

draw_square()
Sign up to request clarification or add additional context in comments.

1 Comment

yeah that works..sorry i am new to python so some learning curve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.