THIS IS PYTHON CODING!! In the Fibonacci series, the first element is 0, and the
ID: 3684775 • Letter: T
Question
THIS IS PYTHON CODING!!
In the Fibonacci series, the first element is 0, and the second element is 1.
Subsequent elements can be computed by adding the previous two elements. For example, the third element is 0+1, or 1, and the fourth element is 1+1, or 2.
Write a WHILE LOOP that asks the user for the element that would like to find the value of, e.g. 5, and the while loop will output the Fibonacci numbers from 1 till the element the user requested.
I figureed out using the if else statement, however, i am stuck on using the while loop! please please help. sample coding with explanations would be great!
Explanation / Answer
nterms = int(input("How many terms? "))
n1 = 0
n2 = 1
count = 2
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence: ")
print(n1)
else:
print("Fibonacci sequence: ")
print(n1,",",n2,end=', ')
while count < nterms :
nth = n1 + n2
print(nth,end=' , ')
n1 = n2
n2 = nth
count += 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.