Matt Zelenka
Matt Zelenka

Reputation: 29

Turtle begin_fill and end_fill with multiple shapes and loops

I need help capping off my for loops. (They can go on forever, yet I need them to be in range of 12 and 10.) My thinking is instead of this

    for i in range(sides):

to do:

    for sides in range(12):
    #and for repeat do 
    for repeat in range(10):

This, in theory, should stop the possibly infinite process of looping, right? The second problem is my end_fill() doesn't fill in each shape with a different color. The outline and inside should be the same, and on a new shape, should be a random color (using randint function).

from turtle import *
import math
import random
#Continue being lazy and not creating turtles, also using a .cfg file to make the window.
colormode(255)
sides = int(input("Enter a number of sides (1-12): "))
repeat = int(input("How many times to repeat? (1-10): "))

#Turtle stuff.
penup()
goto(0,0)
size = 100

angle1 = 360 / sides
angle2 = 360 / repeat
#Here is where I am having issues, I am not sure how much to turn the turtle by do to the needed input
#Also I need to cap this off to only 12 sides and 10 repeats
#As it stands it can go for infinitum.
begin_fill()
for count in range(repeat):
for i in range(sides):
        pendown() #Puts the pen down.
        pensize(5) #Sets the pen to something more visible
        colors1 = random.randint(0, 255) #Random red
        colors2 = random.randint(0, 255) #Random blue
        colors3 = random.randint(0, 255) #Random green
        forward(size) #Goes forward 10 pixels
        left(angle1)
        penup()
#Goes left 360 / sides degrees. So 360 / 10 = 36 degrees.
    pencolor(colors1, colors2, colors3) #Sets pencolor to a random RGB code
    fillcolor(colors1, colors2, colors3) #Sets fillcolor to a random RGB code.
    left(angle2) #Move left 90 degrees afterwards.
    forward(5) #Moves forward 5 times.
    end_fill() #Fills in the shape.

Code also available here: https://gist.github.com/anonymous/3984f7a1a04e9957ea55

Upvotes: 0

Views: 1237

Answers (1)

cdlane
cdlane

Reputation: 41872

Your first problem can be solved using Python 3 turtle's graphic numinput() instead of console input():

numinput(title, prompt, default=None, minval=None, maxval=None)

This will limit the range of the user's input. Your second issue is due to having begin_fill and end_fill at two different indentation levels. Usually, they should be at the same level. Here's a rework of your code with the above changes:

from turtle import Screen, Turtle
import random

screen = Screen()
screen.colormode(255)

sides = screen.numinput("Color Polygons", "Enter a number of sides (1-12)", default=6, minval=1, maxval=12)

if sides is None:  # user cancelled operation
    sides = 6

repeat = screen.numinput("Color Polygons", "How many times to repeat? (1-10)", default=5, minval=1, maxval=10)

if repeat is None:
    repeat = 5

turtle = Turtle()
turtle.speed('fastest')
turtle.pensize(5)
turtle.penup()

size = 100

angle1 = 360 / sides
angle2 = 360 / repeat

for count in range(int(repeat)):
    red = random.randint(0, 255)
    green = random.randint(0, 255)
    blue = random.randint(0, 255)

    turtle.color(red, green, blue)

    turtle.begin_fill()

    for i in range(int(sides)):
        turtle.pendown()
        turtle.forward(size)
        turtle.left(angle1)
        turtle.penup()

    turtle.end_fill()

    turtle.left(angle2)
    turtle.forward(5)


turtle.hideturtle()

screen.exitonclick()

enter image description here

Upvotes: 1

Related Questions