Question

I am trying to create a looping square, and cannot figure out how to get my code to allow me to keep repeating the command of creating squares, times the number input, heres what I have currently.

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())
Était-ce utile?

La solution

You can use for i in range(count_int): to run a piece of code repeatedly given a repeat count in 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())

Autres conseils

You could try doing this

x=1
while x < 10000000:

when you do this anything you type after this will be done again until it has been done 10000000 times. at the end though you have to put this in.

x+=1

Heres am example i made.

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)

then again, you could just put it in a function and tell it to run itself again

def example():
    [insert code]
    example()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top