Previous Lecture Complete and continue  

  Lesson 8: Looping Squares

63 comments
Ivo Ivanov
take a look at this https://repl.it/GBOT/3 ... its pretty cool :D
Chastity Lawson
NEAT !
David
https://trinket.io/python/19003f1644
Nathan Yousif
For some reason it doesn't work for me. It keeps saying '' unindent does not match any outer indentation level''. How can I solve this?
David
did you intent your code correctly?
Marek Ondrejka
https://trinket.io/python/678c063d92
goutham kumar
import turtle

my_turtle=turtle.Turtle()



def square(length,angle):
# my_turtle.speed(5)
my_turtle.circle(100)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)

for i in range(4):
square(50,90)
Sonia Santos
import turtle

my_turtle=turtle.Turtle()

def square(lenght,angle):
my_turtle.forward(lenght)
my_turtle.left (angle)
my_turtle.forward (lenght)
my_turtle.left (angle)
my_turtle.forward(lenght)
my_turtle.left (angle)
my_turtle.forward (lenght)


square(50,90)

def square2():
my_turtle.left(100)
my_turtle.forward(50)
my_turtle.left(90)
my_turtle.forward(50)
my_turtle.left(90)
my_turtle.forward(50)
my_turtle.left(90)
my_turtle.forward(50)

for i in range(50):
square2()


ehhe - I am so proud of myself. Took me time... but I got the result. cool :)
Alex Koo
import turtle
alex=turtle.Turtle()

alex.speed(1000000)

def sqaure(length,angle):
alex.forward(length)
alex.left(angle)
alex.forward(length)
alex.left(angle)
alex.forward(length)
alex.left(angle)
alex.forward(length)



for spin in range(1000):
sqaure(50,90)
alex.left(1)
alsong dunstan
https://repl.it/FpQs/0
Stephen Kane
zeenath Syedda
import turtle

zee = turtle.Turtle()

def square(l, a):
zee.forward(l)
zee.left(a)
zee.forward(l)
zee.left(a)
zee.forward(l)
zee.left(a)
zee.forward(l)


for i in range(4):
square(75,90)
Parthu34
#square fun designs


import turtle
my_turtle=turtle.Turtle()
def square(lenght,angle):

my_turtle.forward(lenght)
my_turtle.left(angle)
my_turtle.forward(lenght)
my_turtle.left(angle)
my_turtle.forward(lenght)
my_turtle.left(angle)
my_turtle.forward(lenght)

for i in range(4):
square(90,90)
my_turtle.left(135)
my_turtle.forward(128)
my_turtle.right(135)
my_turtle.forward(180)
my_turtle.right(135)
my_turtle.forward(128)
my_turtle.left(90)
my_turtle.forward(128)
my_turtle.right(135)
my_turtle.forward(180)
my_turtle.right(135)
my_turtle.forward(128)

Michael Daniels
#draw octagon 10 times
import turtle

my_turtle = turtle.Turtle()

#give the function, square, context
def square(length, angle):

my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)

for i in range(10):
square(100,45)
#this draws an octagon, 10 times

András Takaró
What about this one?
import turtle

bocni = turtle.Turtle()

def square(l, a):
for i in range(4):
bocni.forward(l)
bocni.left(a)

square(100, 90)
cooger
import turtle


import turtle
fredy=turtle.Turtle()

def square(length, angle):
fredy.forward(length)
fredy.right(angle)
fredy.forward(length)
fredy.right(angle)
fredy.forward(length)
fredy.right(angle)
fredy.forward(length)

for i in range(20):
square(90, 135)

great!
Sebastian Furnigel
https://trinket.io/python/4b8c0b022c
Ivan Buglosa
import turtle
myTurtle = turtle.Turtle()

def square(length, angle):
myTurtle.forward(length)
myTurtle.right(angle)
myTurtle.forward(length)
myTurtle.right(angle)
myTurtle.forward(length)
myTurtle.right(angle)
myTurtle.forward(length)
myTurtle.right(angle)


for i in range(4):
square(50,90)


Guys, I want to ask something about my code. The turtle is just making 1 square and doing 4 loops in it. it doesn't go like in the video which it creates 4 different squares. Mine is different, it creates 4 squares overlaping each other. What is wrong?Thanks in advance!
Fosse Lin-Bianco
I believe when you defined the square, the last motion instructed is to turn "some angle". This makes the turtle turn 90 degrees after it completed the square. Therefore when you loop the code, the turtle will turn 90 degrees before starting over again. Hence "re drawing" the same square. Just delete the last line of code that says, "myTurtle.right(angle)" and it should work. Hope this helps;)
Ivan Buglosa
It worked! Thank you so much for your help! :)
Fosse Lin-Bianco
Of course;)
Luftwafte II
Check this out!! :P
import turtle

my_turtle = turtle.Turtle()

#Squ40re
def square(a,b,c):
for i in range(75):
my_turtle.forward(a)
my_turtle.left(c)
for n in range(4):
my_turtle.forward(b)
my_turtle.right(c)


square(110,90,50)
Prince
Hey......... I'm commenting here for the first time (but its not the last one).......
Project 1:
https://repl.it/F7fv/0
Looping squares.....
It's just amazing....
I like it.......
Creatron Omega
import turtle

turtle = turtle.Turtle()

#square function
def square(length, angle):

turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)


for i in range(10):
square(50, 75)
for i in range(10):
suare(100, 50)
#simple but I'm proud of it ;)!
geevan erasga asilom
import turtle

my_turtle = turtle.Turtle()

def square(length,angle):
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)

square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
square(50,30)
KhalidBinAwwad
import turtle


turtle = turtle.Turtle()

def square(length, angle):
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)

for i in range(4):
square(50, 90)

turtle.forward(100)


for i in range(4):
square(40, 90)

turtle.forward(80)

for i in range(4):
square(30, 90)

turtle.forward(60)

for i in range(4):
square(20, 90)

# Creating a bigger square !

turtle.forward(20)

turtle.color('white')

turtle.forward(80)

turtle.left(90)

turtle.back(100)

turtle.color('red')

# Making a bigger square!

turtle.forward(300)

turtle.left(90)

turtle.forward(500)

turtle.left(90)

turtle.forward(300)

turtle.left(90)

turtle.forward(500)



Just something lol
SK MD GALIB
import turtle
j = turtle.Turtle()
j.pencolor("blue")
j.speed(100)
def square(n,p):
j.forward(n)
j.right(p)
j.forward(n)
j.right(p)
j.forward(n)
j.right(p)
j.forward(n)

n = 10
p = 123
for i in range(40):
square(n,p)

Michael Simiyu
i cant continue
Rafeh Qazi
Why not?
tajwayne murray
i forgot the colon at def square
tajwayne murray
import turtle
#def square has to be indented
#for i in range has to be indented as well

k = turtle.Turtle()

def square(l , a)
#l= length
#a=angle

k.forward(l)
k.right(a)
k.forward(l)
k.right(a)
k.forward(l)
k.right(a)
k.forward(l)

for i in range(4):
square(100,90)


its fun if you actually understands it completely. i realize it all works with a bunch of equation you have to remember.

poonam ahir
import turtle

hi = turtle.Turtle()

def sq(length, angle):
hi.forward(length)
hi.left(angle)
hi.forward(length)
hi.left(angle)
hi.forward(length)
hi.left(angle)
hi.forward(length)

for i in range(4):
#hi.forward(100)
sq(20, 30)
hi.forward(100)

for i in range(4):
sq(20, 30)
hi.forward(75)

for i in range(4):
sq(20, 30)
hi.forward(50)

for i in range(4):
sq(20, 30)
hi.forward(25)

for i in range(4):
sq(20, 30)
Haris Rizwan
import turtle

my_turtle=turtle.Turtle()

def square(length, angle):

my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)

for i in range(1000):
square(100 + i*10, 90)
Shaun
import turtle
Oz=turtle.Turtle()

#Oz gets defined from turtle.
#Oz's movements get defined as Square().
#Use the newly defined Square() instead of writing the code over and over.
#Make sure you indent when defining.

def Square(L, A):
#L = Length
#A = Angle

Oz.forward(L)
Oz.right(A)
Oz.forward(L)
Oz.right(A)
Oz.forward(L)
Oz.right(A)
Oz.forward(L)
for S in range(10):
Square(100 , 90)
Xavier Harris-James
https://repl.it/FRRO/0
hope this works if not then the lines look like this.

import turtle
atm = turtle.Turtle()
def square(length,angle):
atm.forward(length)
atm.right(angle)
atm.forward(length)
atm.right(angle)
atm.forward(length)
atm.right(angle)
atm.forward(length)
for x in range(8):
square(50,45)

x
Ryan
my_turtle=turtle.Turtle()

def square(length, angle):

my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)

for i in range(4):
square(50, 90)

Awesome! Very clear video. Loops make total sense now :D
joseph mesa
import turtle

my_turtle = turtle.Turtle()

def square(length, angle):
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)

for i in range(20):
square(30, 70)
PaRuSch
Going more and more fancy.
https://repl.it/FKxr/0
Sebastian Gomez
https://repl.it/EyXR/0
Andres felipe Mora Guerrero
yoo thats so coool! nice
Aman Tyagi
great job man! you're not a beginner, are you?
Ali
Thats cool :)
Sebastian Gomez
Thank you, Ali!
Andrej Radojcic
When i do this it just draws 4 squares in same position. Then i tried to write this(don't pay attention to these weird words it's Serbian):
for nesto in range(4):
square(100, 90)
kvadrat.right(90)
kvadrat.forward (0.5)

And every time i don't get same 4 squares as you ( i tried to change kvadrat.forward many times but any of them isn't same as yours)
Aman Tyagi
# hey Andrej Radojcic, now your code is working
import turtle

kvadrat = turtle.Turtle()

def sqaure(length, angle):
kvadrat.forward(length)
kvadrat.right(angle)
kvadrat.forward(length)
kvadrat.right(angle)
kvadrat.forward(length)
kvadrat.right(angle)
kvadrat.forward(length)



for i in range(2):
sqaure(50, 90)
sqaure(50, 90)

turtle.done()
Praneeth
import turtle
a = turtle.Turtle()
def polygon(length , angle):
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)
a.forward(length)
a.right(angle)

polygon(25 , 90)
a.forward(100)
polygon(25 , 45)

shane
import turtle
my_turtle = turtle . Turtle()
def square(length,angle):



my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)
my_turtle.left(90)
my_turtle.forward(length)


for i in range(4):

square(100, 90)


Billy Harper
Why do my squares keep coming out side by side instead of stacking like yours do? I'm missing something but I can't figure out what.
Roshan Ali
I too... :(
Billy Harper
Hey man, I figured out what I was doing wrong. I had two extra lines of codes when defining 'square'. Not sure how it happened but I removed them and hey presto it worked. Maybe you have done the same?
Ali
can u show me the code u used ?
Billy Harper
Sure...

https://repl.it/Etkz/4

you actually helped me there also as I remade the code again without watching the video. :)
Darren Ong
import turtle
a = turtle.Turtle()
a.speed(0)

def square(length, angle):
for i in range(4):
a.forward(length)
a.right(angle)

for i in range(360):
square(100, 90)
a.right(1)

JUST AMAZING!
Ereatara Kokiri
loops
for e in range(4):
square(100)
David Žiković
import turtle

my_turtle = turtle.Turtle()

def square1(lenght,angle):
my_turtle.forward(lenght)
my_turtle.left(angle)

def square2(lenght,angle):
my_turtle.forward(lenght)
my_turtle.right(angle)

for s in range(5):
square1(50,60)

for s in range(6):
square2(50,60)

square2(50,60)

for s in range(5):
square1(50,60)

my_turtle.left(120)

for s in range(6):
square1(50,60)

square1(50,60)

for s in range(6):
square2(50,60)

my_turtle.right(120)

square2(50,60)

for s in range(7):
square1(50,60)

for s in range(6):
square2(50,60)
Hatim Elsadig Elamin Elhag
https://repl.it/EwjD/0
Diwaker Thakur
import turtle
my_turtle = turtle.Turtle()

def tringle(length, angle):
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
for i in range(10):
tringle(100, 60)
#try this
Anthony Davis
import turtle
j = turtle.Turtle()
def square(length, angle):
j.forward(length)
j.left(angle)
j.forward(length)
j.left(angle)
j.forward(length)
j.left(angle)
j.forward(length)
square(50, 90)
for i in range(4):
square(50, 90)
for i in range(4):
square(100, 90)
for i in range(4):
square(150,90)
for i in range(4):
square(200, 90)
# Im really starting to like this!!!
Robert Spina
import turtle

my_turtle = turtle.Turtle()
def square(length, angle):
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)
my_turtle.right(angle)
my_turtle.forward(length)

for i in range(8):

square(25, 120)
Tim Lange
import turtle

x = turtle.Turtle()

y = x.forward
z = x.right
u = x.left

def crazy(length, angle, anglee):

y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)
y(length)
z(angle)
y(length)
u(anglee)

for i in range(3):
crazy(50, 90, 45)

You'll get a pretty crazy thing.
Anidhito Irmandharu
import turtle

Avant = turtle.Turtle()

def square(xlength ,turn):
Avant.forward(xlength)
Avant.right(turn)
Avant.forward(xlength)
Avant.right(turn)
Avant.forward(xlength)
Avant.right(turn)
Avant.forward(xlength)

# This is A Single
# Flower Petal

#square(200 ,90)
#Avant.right(120)
#square(150 ,90)
#Avant.right(120)

#-----------------

for flowerpetal in range(6):
square(200 ,90)
Avant.right(120)
square(150 ,90)
Avant.right(120)

Same flower , but less lines :)
Lyubomir Ivelinov Petrakiev
hmmm so I was thinking, wouldn't be loops useful to simplify your square?

For example, the basic turtle we learned:

import turtle
my_turtle = turtle.Turtle()
my_turtle.forward(100)
my_turtle.left(90)
my_turtle.forward(100)
my_turtle.left(90)
my_turtle.forward(100)
my_turtle.left(90)
my_turtle.forward(100)

Would be simplified to:

import turtle
my_turtle = turtle.Turtle()
for i in range(4):
my_turtle.forward(100)
my_turtle.left(90)

And the definition:

import turtle
my_turtle = turtle.Turtle()
def square(length, angle):
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)
my_turtle.left(angle)
my_turtle.forward(length)

Would be simplified to:

import turtle
my_turtle = turtle.Turtle()
def square(length, angle):
for i in range(4):
my_turtle.forward(length)
my_turtle.left(angle)

Wouldn't that be useful? I'm not sure but I tried it and the code worked...
Jamie
Ok so I am now a little confused with why when in range of 4 it makes 4 squares of the size value given but 1000000 just over lapped. is it because its to biga number. I pictured like the page would look like graph paper or something.