蟒蛇龟圈

时间:2012-09-15 20:43:05

标签: python loops for-loop turtle-graphics

我正在尝试创建一个循环方块,并且无法弄清楚如何让我的代码允许我不断重复创建方块的命令,输入数字的次数,这是我目前所拥有的。

square_count = input("Enter the number of squares to draw: ")
count_int = int(square_ct)

if count_int > 1:

    turtle.begin_fill()
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.end_fill()

    turtle.up()
    turtle.forward(20)
    turtle.color(random.random(),random.random(), random.random())

3 个答案:

答案 0 :(得分:3)

如果for i in range(count_int):重复计算,您可以使用count_int重复运行一段代码:

if count_int > 1:
    for i in range(count_int):
        turtle.begin_fill()
        turtle.forward(100)
        turtle.right(90)
        turtle.forward(100)
        turtle.right(90)
        turtle.forward(100)
        turtle.right(90)
        turtle.forward(100)
        turtle.end_fill()

        turtle.up()
        turtle.forward(20)
        turtle.color(random.random(),random.random(), random.random())

答案 1 :(得分:0)

您可以尝试这样做

x=1
while x < 10000000:

当您执行此操作后,您将在此之后再次输入任何内容,直到完成10000000次为止。 最后虽然你必须把它放进去。

x+=1

这是我做的榜样。

import turtle
bob = turtle.Turtle()
wn = turtle.Screen()
bob.color("white")
bob.speed(1000000000000000000000000)
wn.bgcolor("black")
x=1
while x < 10000000:
bob.forward(90)
bob.left(89)
bob.forward(1+x)

答案 2 :(得分:-1)

然后,你可以把它放在一个函数中并告诉它再次运行

def example():
    [insert code]
    example()