Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 5: The Fibonacci sequence in Python In IDLE, use the File -> New Window

ID: 3737976 • Letter: P

Question

Problem 5: The Fibonacci sequence in Python

In IDLE, use the File -> New Window menu option to open a new editor window for your code, and save it using the name ps6pr5.py. Make sure to specify the .py extension.

The Fibonacci sequence is one of the most famous sequences in mathematics. The first Fibonacci number (F0) is 1, the second Fibonacci number (F1) is also 1, and each of the remaining Fibonacci numbers can be found by adding the previous two numbers in the sequence:

F0 = 1

F1 = 1

F2 = 1 + 1 = 2

F3 = 1 + 2 = 3

F4 = 2 + 3 = 5

F5 = 3 + 5 = 8

F6 = 5 + 8 = 13

F7 = 8 + 13 = 21

etc.

In this problem, you will write two separate functions. Taken together, these functions will serve as an interactive program that allows the user to work with Fibonacci numbers.

Write a function fib(n) that takes an integer n as input, and that uses a loop to both:

determine and print the first n Fibonacci numbers

compute and return the sum of those numbers

You may assume that the input n is at least 2.

For example:

In this case:

The first 4 values (the numbers 1, 1, 2, and 3) are the first 4 Fibonacci numbers; they are printed by the function.

The final value (the 7) is the sum of those 4 numbers. This sum is returned by the function, which is why we see it when we call the function from the Shell. However, the function itself does not print the sum.

Here’s another example:

In this case, the call fib(7) prints the first 7 Fibonacci numbers, and it returns their sum. However, because we assign the return value to the variable result, the sum is notdisplayed at the bottom of the list of numbers. To see the sum, we can print or evaluate the variable result:

Notes/hints:

Your function must use either a for loop or a while loop.

Because the next Fibonacci number is derived from the two previous numbers, we recommend that you have three different variables for Fibonacci numbers: one for the “current” number (the one that will be printed next) and two variables for the previous two numbers. In addition, you will need a separate variable for the sum of the numbers.

At least some of your variables will need to be initialized before you begin looping, and all of them will need to be updated appropriately inside the loop.

Make sure to test your function for a variety of inputs, starting with n = 2.

Write a function main() that takes no inputs. The function should:

ask the user how many Fibonacci numbers he or she wants to produce

call the fib function to generate and print those numbers

print the sum of the numbers (i.e., the value returned by fib).

The main function itself should not return a value.

Here is one sample interaction with the user, in which we assume that the user enters 4:

Notes/hints:

To get the integer from the user, you should use the input function that we have discussed in lecture, along with the appropriate conversion function (one that will convert the string returned by input to an integer).

fib already prints the requested Fibonacci numbers, so your main function does notneed to print them.

Explanation / Answer

def fibonacci(n):

a = 1

b = 1

sum=2;

print(a);

print(b)

for i in range(2,n):

c = a + b

print(c)

sum+=c

a = b

b = c

return sum

def main():

num = input("How many Fibonacci numbers? ")

sum=fibonacci(num)

print "their sum is ", sum

main()