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

Since the main purpose of this chapter is talking about functions, you must use

ID: 3758092 • Letter: S

Question

Since the main purpose of this chapter is talking about functions, you must use def to define function that can be called by main program. A triple quoted string after the def, docstring, is required for your function documentation. Use a loop to call the function you created, so that the user can display different set until hit -1 to quit, or ended otherwise as you specified. An outer looping need be used, so that program can continue calculate different Fibonacci.

The Fibonacci sequences is: 1, 1, 2, 3, 4, 8, 13, … You can see that the first and second numbers are both 1. Thereafter, each number is the sum of the previous row numbers. Write a function to print the first N numbers of the Fibonacci sequence.

This is the problem that I can't solve. It has to be solved using Python. I am not great at coding, so please keep it as simple as possible.

Explanation / Answer

def fibonacci(n):
"""
Fibonacci Function is used to print the numbers in fibonacci sequence up to the value of n.
"""
x = 1
y = 1
z = x+y
while(z<n):
print str(z)+',',
x,y = y,z
z = x+y
  

while(True):
n = int(raw_input('Input the value of n for fibonacci function(-1 to exit)'))
if n==-1:
break;
else:
fibonacci(n)
print