79.8k views
0 votes
Using Python, Write a function whose input is which term of the fibonacci sequence the user would like toknow, and whose output is that term in the fibonacci sequence. For example, ifyou called your function Fib, Fib(1) = 0, Fib(2) = 1, Fib(3) = 1, Fib(4) = 2,and so on.

User Fulldump
by
7.5k points

1 Answer

3 votes

Answer:

#here is code in python

#recursive function to find nth Fibonacci term

def nth_fib_term(n):

#if input term is less than 0

if n<0:

print("wronng input")

# First Fibonacci term is 0

elif n==0:

return 0

# Second Fibonacci term is 1

elif n==1:

return 1

else:

#find the nth term of Fibonacci sequence

return nth_fib_term(n-1)+nth_fib_term(n-2)

n=int(input("please enter the term:"))

print(n,"th Fibonacci term is: ")

print(nth_fib_term(n))

Explanation;

Read the term "n" which user wants to know of Fibonacci sequence.If term term "n" is less than 0 then it will print "wrong input". Otherwise it will calculate nth term of Fibonacci sequence recursively.After that function nth_fib_term(n) will return the term. Print that term

Output:

please enter the term:7

7 th Fibonacci term is:

13

User LLuz
by
7.4k points