所以我需要一些帮助来限制for循环(它们可以永远持续下去,但我需要它们在12和10的范围内。)所以我的想法是这个
for i in range(sides):
代替
for sides in range(12):
#and for repeat do
for repeat in range(10):
理论上应该停止可能无限循环的过程吗?无论如何继续第二个问题,我的end_fill()不会用不同的颜色填充每个形状(轮廓和内部应该是相同的,并且在新的形状上应该是随机颜色(使用randint函数))< / p>
此处提供的代码:https://gist.github.com/anonymous/3984f7a1a04e9957ea55
答案 0 :(得分:1)
您可以使用Python 3 turtle的图形numinput()
而不是控制台input()
解决第一个问题:
numinput(title, prompt, default=None, minval=None, maxval=None)
这将限制用户输入的范围。您的第二个问题是由于begin_fill
和end_fill
处于两个不同的缩进级别。通常,它们应该处于同一级别。经过上述更改,这是对代码的重做:
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()