It’s the python challenge where they want you to code a Fibonacci sequence. I wanted to challenge myself and get it to work using a loop; I got it to work but I don’t understand why “num < 3” is throwing the sequence up to 8.
for num in lst:
- There are 3 three item in lst: [1, 1, 2]
if num < 3
This evaluates to False because all num in lst are less than 3. Therefore, the program will append
lst.append(lst[-1] + lst[-2]) 3 times, on lst because there are three items in the lst (e.g, for num in lst).
Therefore, on the first iteration 3 gets appended, on the second iteration 5 gets appended, and on the third iteration 8 gets appended. Does that makes sense?
Let me know if that answers your question or if you still need more explanation.
your ‘append_sum’ returns that result becouse when you ask the function you declared 3 items as lst.
Your function appends for/loop results to the same variable lst , so: you have a 5 items as final result and the sequence goes up to 8.