Looping circles with specific functions - python

Answered
I need to create a function named "caterpillar()", this function is used to join my draw_circle() function, and draw_line() functions together to create a caterpullar. I'm stuck on trying to create the three circles needed for the caterpillars body using draw_circle within a while loop. This is all my code so far:
from turtle import *
import turtle
def moveto(x,y):
penup()
goto(x,y)
pendown()
def draw_circle(xpos,ypos,radius,colour):
moveto(xpos,ypos)
circle(radius)
turtle.fillcolor(colour)
def draw_line(x1, y1, x2, y2):
penup()
goto(x1,y1)
pendown()
goto(x2,y2)
def draw_square(x,y,length,colour):
moveto(x,y)
forward(length)
right(90)
forward(length)
right(90)
forward(length)
right(90)
forward(length)
turtle.fillcolor(colour)
def caterpillar():
draw_line(0,30,-20,-15) # feelers
draw_line(0,30,20,-15) # feelers
draw_line(60,30,40,-15) # feelers
draw_line(60,30,80,-15) # feelers
draw_line(120,30,100,-15) # feelers
draw_line(120,30,140,-15) # feelers
for _ in range(3) : # 3 body circles
xpos = 0
ypos = 0
radius = 30
turtle.begin_fill()
draw_circle(0,0,30,"green")
turtle.end_fill()
xpos = xpos + (radius*2)
caterpillar()
I am stuck on the last part under "for _ in range(3)" - I need to loop three circles using the draw_circle function at these specific coords:
Caterpillar
I've been stuck on this for hours, any help would be much appreciated!
Edit:
Also forgot to mention, that i keep getting the error "File "C:\Users\Rekesh\Desktop\caterpillar\1.py", line 48, in caterpillar
xpos = xpos +(radius*2)
UnboundLocalError: local variable 'xpos' referenced before assignment
When I use xpos = xpos, i'm not sure if this is needed.

Related

Drawing with turtle module and while loops in Python 3

I'm trying to get this code to request an input: number of points on a star, then use the turtle module to draw each respective star. Each one is meant to be in a distinct location and have a distinct line and fill color. This code won't draw second and third stars, though it'll draw the first one fine. Also, the fill function only fills the "arms" of the star, not the body. I'm attaching some code and a screenshot of when I tried it with the three inputs: 5, 7, and 9.
from turtle import *
def start():
"""This function initializes the window and the turtle.
Do not modify this function.
"""
setup(564, 564)
width(7)
side_length = 60 # Also the radius of a circle enclosed by the star.
penup()
goto(0, -side_length) # Start at the bottom of the star.
pendown()
def main():
points = int(input('How many points would you like on your star? '))
A = 360 / points
B = 2 * A
x = 0
side_length = 60
while x < points:
color('blue', 'red')
begin_fill()
left(A)
forward(side_length)
right(B)
forward(side_length)
x += 1
end_fill()
penup()
goto(2*side_length, 2*side_length)
pendown()
points = int(input('How many points would you like on your star? '))
while x < points:
color('green', 'yellow')
begin_fill()
left(A)
forward(side_length)
right(B)
forward(side_length)
x += 1
end_fill()
penup()
goto(-2*side_length, 2*side_length)
pendown()
points = int(input('How many points would you like on your star? '))
while x < points:
color('orange', 'purple')
begin_fill()
left(A)
forward(side_length)
right(B)
forward(side_length)
x += 1
end_fill()
# Do not change anything after this line.
if __name__ == '__main__':
start()
main()
done()

How to get turtle to STOP after 5 circles! Python 3.x

I need my turtle to stop after 4 circles but he won't do it! Can anyone help? I wouldn't ask unless I was really stuck and I have been researching for a while.
from turtle import *
import time
### Positioning
xpos= -250
ypos= -250
radius= 40
speed(10)
###Program main function
while radius == 40:
pu()
goto (xpos, ypos)
pd()
begin_fill()
color("red")
circle(radius)
end_fill()
xpos = xpos + (radius*2)
while radius == 40 - you never change radius, so it will never stop. Instead, loop over an iterable like range():
for _ in range(5):
Remember, you don't have to use the loop variable within the loop. It's okay to use it as nothing other than a counter.
The following code would draw your N times, where you would replace N with the number of circles you want it to draw:
from turtle import *
import time
### Positioning
xpos= -250
ypos= -250
radius= 40
speed(10)
currentIterateCount = N
###Program main function
while currentIterateCount != 0:
pu()
goto (xpos, ypos)
pd()
begin_fill()
color("red")
circle(radius)
end_fill()
xpos = xpos + (radius*2)
currentIterateCount--;
The reason that it drew more circles than you wanted is because you were checking against a variable that never changed.
By this I mean you were seeing if radius changed, but never changing it.

How to get the right output for this turtle function?

I have a logic error for this function. I need to write a function called spikes() that draw
lines radiating from a common starting point. It takes three parameters, which are
numLines for number of lines to draw, lengthIncr for the length of the first line and
increase in length of the successive lines, and angle that goes clockwise and it is the angle between successive lines. I use the latest version of Python (3.4.2). Also, the function spides must repeatedly call the function drawLine(). I think the error is the call function for drawLine in the function spikes, but I don't know how to fix it. The output I get is a very long curve line that goes downward forever. The real output I should get is spikes. Here is the code:
#Question 14 Part a-
import turtle
s = turtle.Screen()
t = turtle.Turtle()
def drawLine(t, length):
t.pendown()
t.forward(length)
t.penup()
length = 50
drawLine(t, length)
#Question 14 Part b-
def spikes(numLines, lengthIncr, angle):
for i in range(numLines):
drawLine(t, lengthIncr * i)
t.right(angle)
print(spikes(36, 25, 5))
#Output I should get: '''
I'm not an expert on turtle, but is this what you're looking for?
import turtle
s = turtle.Screen()
t = turtle.Turtle()
def drawLine(t, length):
t.pendown()
t.forward(length)
t.penup()
def there_and_back(t, length):
drawLine(t, length)
t.penup()
t.right(180)
t.forward(length)
t.right(180)
t.pendown()
length = 50
#Question 14 Part b-
def spikes(numLines, lengthIncr, angle):
for i in range(numLines):
length = lengthIncr * i
there_and_back(t, length)
t.right(angle)
print(spikes(36, 25, 5))

How can I fill in turtle coordinates from a list using Python?

I need to know how I can fix this code up so that when the list called "redlist" is called which has a list of coordinates, it fills those points when the move function is called. This is what I have so far:
redlist=[]
def move():
penup()
if color()[0]=="red":
#coordinates of redlist should be filled here
def paint():
pendown()
if color()[0]=="red":
begin_fill
redlist.append(pos())
I do not know what coordinates you mean but I call them x and y
redlist=[]
def move():
penup()
if color()[0]=="red":
x = 1 # replace this line
y = 2 # replace this line
redlist.append((x, y)) # you can access them by #x, y = redlist[-1]
#coordinates of redlist should be filled here
def paint():
pendown()
if color()[0]=="red":
begin_fill
redlist.append(pos())

How Can I Fill These Squares in Turtle - Python

I am trying to fill the color in these squares:
Right now the turtle only fills the corners of theses squares, not the entire square.
Here is my code:
import turtle
import time
import random
print ("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = input("Enter the side number of the shape you want to draw: ")
if num_str.isdigit():
squares = int(num_str)
angle = 180 - 180*(squares-2)/squares
turtle.up
x = 0
y = 0
turtle.setpos(x,y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(),random.random(), random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
turtle.end_fill()
time.sleep(11)
turtle.bye()
I've tried moving around turtle.begin_fill() and end_fill() in numerous locations with no luckā€¦ Using Python 3.2.3, thanks.
I haven't really used turtle, but it looks like this may be what you want to do. Correct me if I've assumed the wrong functionality for these calls:
turtle.begin_fill() # Begin the fill process.
turtle.down() # "Pen" down?
for i in range(squares): # For each edge of the shape
turtle.forward(40) # Move forward 40 units
turtle.left(angle) # Turn ready for the next edge
turtle.up() # Pen up
turtle.end_fill() # End fill.
You're drawing a series of triangles, using begin_fill() and end_fill() for each one. What you can probably do is move your calls to begin_fill() and end_fill() outside the inner loop, so you draw a full square and then ask for it to be filled.
Use fill
t.begin_fill()
t.color("red")
for x in range(4):
t.fd(100)
t.rt(90)
t.end_fill()
Along with moving begin_fill() and end_fill() outside the loop, as several folks have mentioned, you've other issues with your code. For example, this is a no-op:
turtle.up
I.e. it doesn't do anything. (Missing parentheses.) This test:
if num_str.isdigit():
Doesn't do much for you as there is no else clause to handle the error. (I.e. when it isn't a number, the next statement simply uses the string as a number and fails.) This calculation seems a bit too complicated:
angle = 180 - 180*(squares-2)/squares
And finally there should be a cleaner way to exit the program. Let's address all these issues:
from turtle import Screen, Turtle
from random import random
NUMBER_SHAPES = 8
print("This program draws shapes based on the number you enter in a uniform pattern.")
num_str = ""
while not num_str.isdigit():
num_str = input("Enter the side number of the shape you want to draw: ")
sides = int(num_str)
angle = 360 / sides
delta_distance = 0
delta_angle = 0
screen = Screen()
turtle = Turtle()
for x in range(NUMBER_SHAPES):
turtle.color(random(), random(), random())
turtle.penup()
delta_distance += 5
turtle.forward(delta_distance)
delta_angle += 5
turtle.left(delta_angle)
turtle.pendown()
turtle.begin_fill()
for _ in range(sides):
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
turtle.end_fill()
screen.exitonclick()

Categories

Resources