CPSC120A
Fundamentals of Computer Science

Activity 6

Accumulator

Loop Game

Complete the third level of the Loop Game.

Challenge

Complete the fourth, and final, level of the Loop Game.


Average

Write a Python program that allows the user to computer the average of a series of numbers. The program should prompt the user for the length of the series and then prompt the user for each of the numbers in the series.

Example

Enter number of numbers: 5
Enter number: 2
Enter number: 5
Enter number: 4
Enter number: 3
Enter number: 1
The average is: 3.0

Circular Spiral

Previously we created spirals that looked a little jagged (or really jagged). One way we can solve this problem is using circles in Turtle, and increasing the radius for each external circle.

Details

Create a Python program that draws a circular spiral. The program should prompt the user the number of segments to draw. You should draw a series of quarter-circles (created using turtle.circle(radius, 90)). Each quarter circle should have a radius that is double the radius of the previous circle drawn.

Example

Enter number of quarter circles: 7  
  • Hard coding this shape will be impossible. You need to use a loop and the accumulator.
  • You won't be adding in this version of the accumulator. We want to be doubling the radius after each iteration, so we should be multiplying.

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, \ldots$$

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

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
  • Because the program should work for any length sequence, you will need a loop to compute each of the numbers in the sequence.
  • What is different between this and other uses of an accumulator is that it needs the previous two iterations to compute the next iteration. To do this, keep two variables, one for the previous value and one for the one 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.