Im trying to learn code with Programiz, the challenge is to find and print the numbers in a fibonacci sequence that is less than the number you input. Here is the code, and so far it works for inputting 5 and 6, with both of those inputs it stops at 3 and 5 respectivly in the sequence, but any number higher than that and it keeps going. I cant figure out why. I tried to follow the loop manually. I Input 7, it should loop 6 times (the range is 1,7, but it stops at 7).
loop 1=print t1(1), result=t1(1)+t2(1), t1=t2(1), t2=result(2)
loop 2=print t1(1), result=t1(1)+t2(2), t1=t2(2), t2=result(3)
loop 3=print t1(2), result=t1(2)+t2(3), t1=t2(3), t2=result(5)
loop 4=print t1(3), result=t1(3)+t2(5), t1=t2(5), t2=result(8)
loop 5=print t1(5), result=t1(5)+t2(8), t1=t2(8), t2=result(13)
shouldnt it stop here because t1=8 which is greater than n(7), closing the while loop
n = int(input())
t1 = 1
t2 = 1
result = 0
while t1<n:
for num in range (1,n):
print(t1)
result=t1+t2
t1=t2
t2=result
Sorry for the long question, I just figured I should explain my thought process on troubleshooting it, if anyone can help it would be appreciated.
Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.