CPSC120
Fundamentals of Computer Science

Activity 10

Accumulator

Loop Game

Complete the third level of the Loop Game.

Circular Spiral

Previously we created spirals that looked jagged. We can create smooth spirals by drawing circles with increasing radii.

Details

Create a Python program that draws a circular spiral. The program should prompt the user the number of segments to draw. The program should use a nested loop to draw a series of quarter-circles. Each quarter-circle should have a circumference that is double the circumference of the previous quarter-circle drawn.

Example

Enter number of quarter circles:

7

spiral

Hint

  • A simple way to draw is a circle is to draw a 36 sided polygon. To draw a quarter of a circle, change the stop value of a circle-drawing loop so that it only draws 36 / 4 = 9 sides.
  • Draw multiple quarter-circles, by putting the loop that draws a quarter-circle inside of another loop.
  • Use an accumulator to store the distance the turtle moves when drawing each quarter-circle. Update the accumulator outside of the loop that draws the quarter-circles.

Fibonacci

The Fibonacci sequence is a sequence of numbers that looks like:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, …

Each number in the sequence, after the first two, is the sum of the previous two numbers.

Details

Create a Python program that prints the Fibonacci sequence. The program should prompt the user to enter the length of the series to print.

Example

Enter the sequence length:

8

1 1 2 3 5 8 13 21

Hint

  • Because the program should work for any length sequence, you will need a loop to compute each of the numbers.
  • What is different between this and other uses of an accumulator is that it requires the previous two iterations to calculate the next iteration. To do this, use two variables, one for previous value and one for the value before the previous.
  • Pay attention to how the program will work for small numbers. If the user enters 1, it should just print 1. If the user enters 2, it should print 1 1.

Challenge

Combine your spiral program and your Fibonacci program to create a golden spiral. Create a program that draws a golden spiral, including the boxes but excluding the numbers, like the illustration below.

fibonacci spiral