Python Forum
Fibonacci Sequence on Turtle - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Fibonacci Sequence on Turtle (/thread-1880.html)



Fibonacci Sequence on Turtle - JRod - Feb-01-2017

Hey, 
I am trying to draw a series of fibonacci squares using turtles.
I am new to python but am using loops to try to draw them.
This is what I have so far...

import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")

squares = input("How many squares do you want?")

n = 10
for sqs in range(int(squares)):
   for i in range(6):  #it has 6 turns (not 4) because the next square begins from the corner diagonal to the corner the preceding square began
      tess.forward(n)
      tess.left(90)
   tess.right(90)      #this points the turtle in the right direction to start the next square!

wn.mainloop()
I can code the fibonacci sequence to get integer terms but am stuck on how to nest this within my loop so that each square is drawn with side lengths in accordance with the fibonacci sequence.

Can anyone help?!?

I have added some other code but it still is not increasing the size of each square as it goes on...

import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")

squares = input("How many squares do you want?")

def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))

n = 10
for sqs in range(int(squares)):
for i in range(6):
tess.forward(recur_fibo(n))
tess.left(90)
tess.right(90)


wn.mainloop()


RE: Fibonacci Sequence on Turtle - ichabod801 - Feb-01-2017

You just need to replace the sqs loop with the loop that generates the Fibonacci sequence. Then you just multiply the current Fibonacci number by n in the forward statement.


RE: Fibonacci Sequence on Turtle - Larz60+ - Feb-02-2017

Please use code tags! I added them for you the first time, and you didn't follow up


RE: Fibonacci Sequence on Turtle - JRod - Feb-02-2017

Ok thanks, I will give that a go today.
Any sorry for not including python tags. Will do so from now on.


RE: Fibonacci Sequence on Turtle - JRod - Feb-02-2017

Ok... after a bit more thinking I think I have cracked it!

#fib count

from turtle import *

prev=0
start=1
fibno=1
sqs = input("How many Squares do you want? ")
for i in range(int(sqs)):
   # Show numbers to make sure things are correct...
   print(str(i)+". "+str(fibno))

   # Draw a square
   color('red')
   for f in range(6):
       forward(5*fibno)
       if f<5:right(90)

   # Sort out numbers
   fibno=start+prev
   prev=start
   start=fibno



RE: Fibonacci Sequence on Turtle - ichabod801 - Feb-02-2017

That should work. Note that you don't really need the start variable. With just fibno and prev, you can do this:

prev, fibno = fibno, fibno + prev
The tuple on the right is calculated, and then assigned to the variables in the tuple on the left. This is a handy way to swap values that is particularly useful when calculating the Fibonacci sequence.


RE: Fibonacci Sequence on Turtle - JRod - Feb-03-2017

Thank you!
I will give that a try.


RE: Fibonacci Sequence on Turtle - JRod - Feb-04-2017

Ok, I have just been learning about defining functions and calling them, so I tried to use that idea with this code. ichabod801 I used your tuples to size the squares.

import turtle

prev = 0
fibno = 1
john = turtle.Turtle()

sqs = input("How many Squares do you want? ")

def draw_square(t, sz):
   for i in range (6):
       t.forward(sz)
       t.left(90)
   
for i in range(int(sqs)):
   john.right(90)
   draw_square(john, fibno*5)
   prev, fibno = fibno, fibno+prev



RE: Fibonacci Sequence on Turtle - ichabod801 - Feb-04-2017

Very good. Note that you can also make the loop over range(int(sqs)) into a function, with sqs as a parameter. You could then do the initialization of prev, fibno, and john in that function.

It's also good to use more descriptive variable names. squares for sqs, size for sz, square and side for the two i's. It makes your code clearer and easier to read.


RE: Fibonacci Sequence on Turtle - JRod - Feb-06-2017

Thanks for the advice! As a beginner little tips like that are brilliant. Cheers!