Python Fibonacci sequence- while loop is not giving me the right result. I have tried changing the iterator but to no avail?
Question:
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(find)
fibFind(7)
I have been having issues with the Fibonacci sequence- I have searched for the 7th number- the result should be 13. Where am I going wrong with the logic?
Thanks in advance for answer and explanation.
Answers:
You need to print fib_num
, not find
.
def fibFind(N):
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(fib_num)
fibFind(7)
Better yet, return the fibonacci number from the function after the computation is done.
def fibFind(N):
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
return fib_num
print(fibFind(7))
Hope you are searching for this.
def fibFind(N):
num1 = 0
num2 = 1
find = 0
fib_num = 0
print("Fibonacci sequence:")
while find <= N:
print(num1)
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
fibFind(7)
The first two elements of the sequence could be done outside the loop :
# xn = xn−1 + xn−2
def fibFind(N):
num1 = 0
num2 = 1
find = 2
fib_num = 0
print(fib_num)
fib_num = fib_num + 1
print(fib_num)
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(fib_num)
fibFind(7)
You are printing "find" which you used for iterating the loop from 2. You need to print the "fib_num", the variable that you are using to store the fibonacci number.
def fibFind(N) :
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(fib_num)
fibFind(7)
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(find)
fibFind(7)
I have been having issues with the Fibonacci sequence- I have searched for the 7th number- the result should be 13. Where am I going wrong with the logic?
Thanks in advance for answer and explanation.
You need to print fib_num
, not find
.
def fibFind(N):
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(fib_num)
fibFind(7)
Better yet, return the fibonacci number from the function after the computation is done.
def fibFind(N):
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
return fib_num
print(fibFind(7))
Hope you are searching for this.
def fibFind(N):
num1 = 0
num2 = 1
find = 0
fib_num = 0
print("Fibonacci sequence:")
while find <= N:
print(num1)
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
fibFind(7)
The first two elements of the sequence could be done outside the loop :
# xn = xn−1 + xn−2
def fibFind(N):
num1 = 0
num2 = 1
find = 2
fib_num = 0
print(fib_num)
fib_num = fib_num + 1
print(fib_num)
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(fib_num)
fibFind(7)
You are printing "find" which you used for iterating the loop from 2. You need to print the "fib_num", the variable that you are using to store the fibonacci number.
def fibFind(N) :
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
print(fib_num)
fibFind(7)