Reputation: 31
I'm doing some practice problems where I write code so turtle can draw a square, circle, polygon, etc. The issue is that when I try to run my code, turtle is either:
I'm using Python 3.6 on Spyder and have tried using turtle.mainloop()
and turtle.done()
at the end of each section but I keep running into the same problem.
Here is my code:
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
turtle.done()
for i in range(4):
print("Hello!")
for i in range(4):
bob.fd(100)
bob.lt(90)
turtle.done()
t = turtle.Turtle()
def square(t):
print(t)
t.fd(100)
t.lt(90)
t.fd(100)
t.lt(90)
t.fd(100)
t.lt(90)
t.fd(100)
t.lt(90)
turtle.done()
square(bob)
turtle.done()
t = turtle.Turtle()
def square(t):
print(t)
for i in range(4):
t.fd(100)
t.lt(90)
turtle.mainloop()
turtle.done()
t = turtle.Turtle()
def square(t, length):
print(t)
for i in range(4):
t.fd(length)
t.lt(90)
square(t, 200)
turtle.done()
t = turtle.Turtle()
def polygon(t, length, n):
print(t)
for i in range(4):
t.fd(length)
t.lt(360/n)
polygon(t, t = 200, n = 12)
turtle.done()
import math
def circle(t, r):
circumference = 2 * math.pi * r
n = 100
length = circumference / n
polygon(t, length, n)
circle(t, 100)
turtle.done()
"""draws a circle in turtle"""
Upvotes: 1
Views: 1702
Reputation: 41872
The multiple turtle.done()
statements, when there should only be one, and the fact that the various pieces of code don't take into account where the other pieces of code have drawn, make this look like it should be a collection of individual programs in individual files:
Program 1:
import turtle
bob = turtle.Turtle()
print(bob)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
for i in range(4):
print("Hello!")
turtle.done()
Program 2:
import turtle
bob = turtle.Turtle()
for i in range(4):
bob.fd(100)
bob.lt(90)
turtle.done()
Program 3:
import turtle
def square(t):
print(t)
t.fd(100)
t.lt(90)
t.fd(100)
t.lt(90)
t.fd(100)
t.lt(90)
t.fd(100)
t.lt(90)
bob = turtle.Turtle()
square(bob)
turtle.done()
Program 4:
import turtle
def square(t):
print(t)
for i in range(4):
t.fd(100)
t.lt(90)
t = turtle.Turtle()
square(t)
turtle.mainloop()
Program 5:
import turtle
def square(t, length):
print(t)
for i in range(4):
t.fd(length)
t.lt(90)
t = turtle.Turtle()
square(t, 200)
turtle.done()
Program 6:
import turtle
import math
def polygon(t, length, n):
print(t)
for i in range(n):
t.fd(length)
t.lt(360 / n)
t = turtle.Turtle()
polygon(t, length=50, n=12)
def circle(t, r):
"""draws a circle in turtle"""
circumference = 2 * math.pi * r
n = 100
length = circumference / n
polygon(t, length, n)
circle(t, 100)
turtle.done()
Try running these as separate programs in separate files and see if turtle works any better for you.
Upvotes: 1
Reputation: 6426
You're telling the computer how to do things, but you never actually tell it to do them. Run circle(t)
etc. to do this.
You're not consistently running all the things you want to. Read through your code carefully and make sure you're always running mainloop
etc..
Upvotes: 0