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.
JavaScript
x
19
19
1
square_count = input("Enter the number of squares to draw: ")
2
count_int = int(square_ct)
3
4
if count_int > 1:
5
6
turtle.begin_fill()
7
turtle.forward(100)
8
turtle.right(90)
9
turtle.forward(100)
10
turtle.right(90)
11
turtle.forward(100)
12
turtle.right(90)
13
turtle.forward(100)
14
turtle.end_fill()
15
16
turtle.up()
17
turtle.forward(20)
18
turtle.color(random.random(),random.random(), random.random())
19
Advertisement
Answer
You can use for i in range(count_int):
to run a piece of code repeatedly given a repeat count in count_int
:
JavaScript
1
16
16
1
if count_int > 1:
2
for i in range(count_int):
3
turtle.begin_fill()
4
turtle.forward(100)
5
turtle.right(90)
6
turtle.forward(100)
7
turtle.right(90)
8
turtle.forward(100)
9
turtle.right(90)
10
turtle.forward(100)
11
turtle.end_fill()
12
13
turtle.up()
14
turtle.forward(20)
15
turtle.color(random.random(),random.random(), random.random())
16