Issue
What's wrong with this code? I tried to run this code but in the output' AttributeError: module 'turtle' has no attribute 'screen'
is shown:
import turtle
def draw_square():
window = turtle.screen()
window.bgcolor("red")
brad = turtle.turtle()
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
window.exitonclick()
draw_square()
turtle.done()
turtle.bye()
Solution
You have a few issues in your code. First, this:
window = turtle.screen()
should be this:
window = turtle.Screen()
Ditto here:
brad = turtle.turtle()
which should be:
brad = turtle.Turtle()
Finally, you have a redundancy here:
window.exitonclick()
...
turtle.done()
As both exitonclick()
and done()
serve the same purpose but in different ways. Style-wise, I would setup your screen and turtle outside of draw_square()
and just have that function do what it says it does, draw a square -- below is how I would go about writing this program:
from turtle import Screen, Turtle
def draw_square(turtle):
for _ in range(4):
turtle.forward(100)
turtle.right(90)
window = Screen()
window.bgcolor("red")
brad = Turtle()
draw_square(brad)
window.exitonclick()
Answered By - cdlane
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.