Program Requirements For part 1, you must write a single program with two functi
ID: 3597263 • Letter: P
Question
Program Requirements For part 1, you must write a single program with two functions: . fib(): Takes a single integer argument num.terms greater than or equal to 1 and for each of the num.terms terms of the Fibonacci sequence prints 1) the number of the term-i.e., 1, 2, 3..., 2) the corresponding Fibonacci number, and 3) the ratio of the current and previous Fi- bonacci numbers as shown below. The ratio, printed only for x1 and higher, is an approximation of the golden ratio 1.61803398875 ·main ( ) : Prompts the user for the number of terms s/he wants to find and calls fib ( ) i Enter the number of terms you want to find: 2 3 2 1 1.0 i Enter the number of terms you want to find: 9 3 2 1 1.0 43 2 2.0 s 4 3 1.5 6 5 51.66666666667 76 8 1.6 7 13 1.625 9 8 21 1.61538461538 9 34 1.61904761905 The last line of your program should be a call to main). Don't forget to include docstrings! How many terms are needed to obtain an approximation to the golden ratio accurate to 4 decimal places? Save your program as gold_ratio.py.Explanation / Answer
def fib(n):
'''
program to compute fibonachi number till n and print ratio of fibN with fib n-1
'''
if n >= 1:
print("1 1")
if n >= 2:
print("2 1 1.0")
first = 1
second = 1
for i in range(3, n+1):
fibN = first+second
ratio = float(fibN)/second
print(str(i) + " " + str(fibN) + " " + str(ratio))
first = second
second = fibN
def main():
n = int(input("Enter the number of terms you want to find: "))
fib(n)
if __name__ == '__main__':
main()
# copy pastable code: https://paste.ee/p/jYdg7
'''
Sample run
'''
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.