0

我正在尝试创建一个循环方块,但无法弄清楚如何让我的代码允许我继续重复创建方块的命令,乘以数字输入,这就是我目前所拥有的。

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())
4

3 回答 3

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())
于 2012-09-15T20:45:13.287 回答
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)
于 2013-05-10T09:24:20.643 回答
-1

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

def example():
    [insert code]
    example()
于 2015-10-13T21:00:47.230 回答