0

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

3 Answers 3

3

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())
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a better way to write turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) as they are same values repeating multiple times?
@DebRaj: See Python 3: Can we avoid repeating an instance name when calling several of its methods?, which is probably why you posted your comment in the first place.
@Martin Pieters The link is about a varied movement. I wanted it to be for a similar movement reputation. I found defining a method is helpful like : def draw_square(some_draw): for i in range (0, 4): some_draw.forward(100) some_draw.right(90) and then calling it as draw_square(my_drawing_name) will do the trick.
0

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)

Comments

-1

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

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

1 Comment

How would you suggest preventing this from executing infinitely?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.