programming-fundamentals
  • Introduction
  • Lesson 1
    • Writing Your First Program
    • Data Types
    • Built In Functions
    • If Statements
    • Using Git and Github
    • Primer
    • Questions
    • Tip Calculator
    • GPA Calculator (optional)
  • Lesson 2
    • Modules
    • Lists
    • Data Structures
    • More On Data Structures
    • Questions
    • Madame Zamad
    • Mad Libs
  • Lesson 3
    • While Loops
    • For Loops
    • Loops and Lists
    • Special Commands
    • Questions
    • TODO List
    • Fibonacci (optional)
  • Lesson 4
    • Functions
    • More Functions
    • Error Handling
    • File I/O
    • Questions
    • Sudoku
Powered by GitBook
On this page
  • Setup
  • Requirements
  • General
  • Loops
  • Tips

Was this helpful?

  1. Lesson 3

Fibonacci (optional)

PreviousTODO ListNextFunctions

Last updated 5 years ago

Was this helpful?

This is an optional assignment and can be a little tricky. The Fibonacci sequence is an infinite sequence of numbers that starts with 0 and 1 and then adds the previous number with the current to get the next number.

round 1:
0 <-- previous
1 <-- current
1 <-- new
round 2:
0
1 <-- previous
1 <-- current
2 <-- new

This sequence has many real-world applications and is even a naturally occurring phenomenon in plants and animals. This assignment will not introduce you to any real-world applications but is a fun puzzle to solve that will allow you to flex your new loop-building muscles.

Setup

Fork the following repository and clone your copy (if you haven't already for this lesson).

Open fibonacci.py in your text editor.

Requirements

General

  • ask the user if they want to:

    1) calculate the nth Fibonacci number
    2) calculate the last Fibonacci number less than or equal to n
    3) quit

Loops

  • solve the selected option using loops. Each option lends itself more to a

    certain loop type. It is your job to find out which one goes with which

Tips

  • the first 10 Fibonacci numbers are:

    0 1 1 2 3 5 8 13 21 34
  • an efficient algorithm will require you to swap the values of two variables.

    You can do this using a temporary variable. Since this is a bit tricky, I will

    provide you with the part you will need.

    temp = current
    current += previous
    previous = temp

Fibonacci