Fibonacci's Sequence with a Recursive Loop
Fibonacci's sequence is a famous sequence in which the "n"th term is the previous two terms added together. Here I am programing this sequence using a recursive loop.
def fibonacci_loop(n):
if n <= 1:
return n #you must return n if n<=1 otherwise term will be negative
else:
return(fibonacci_loop(n-1) + fibonacci_loop(n-2))
nterms= 10 #the user can put a term number in here and this loop will list the terms in the fibonacci sequence until that term
if nterms <= 0: #term cannot be negative
print("Please enter a positive integer. Negatives are not accepted.")
else:
for i in range(nterms): #if the integer is positive, the function will be able to list until "n"th term
print(fibonacci_loop(i))