Python Forum
Fibonacci Sequence on Turtle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fibonacci Sequence on Turtle
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Please use code tags! I added them for you the first time, and you didn't follow up
Reply
#4
Ok thanks, I will give that a go today.
Any sorry for not including python tags. Will do so from now on.
Reply
#5
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
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Thank you!
I will give that a try.
Reply
#8
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
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
Thanks for the advice! As a beginner little tips like that are brilliant. Cheers!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fibonacci Yasunaga 7 2,865 May-16-2021, 02:36 PM
Last Post: csr
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,129 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,544 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU
  fibonacci ***Time limit exceeded*** frequency 18 10,056 Nov-29-2018, 09:03 PM
Last Post: frequency
  Fibonacci sequence Darbandiman123 2 2,667 Sep-26-2018, 02:32 PM
Last Post: Darbandiman123
  Help debug my fibonacci implementation dineshpabbi10 6 3,923 May-16-2018, 12:12 PM
Last Post: dineshpabbi10

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020