Help in python code

Hy I am new in python and I stuck in this code. I didn’t get how the second part after while loop worked. would anyone explain to me please.
Thank you!

n = 6
a , b, c = 1, 1, 1
cnt = 2
while (cnt < n):
cnt += 1
a = b + c + 1
c = b
b = a
print(a)

the answer it return is 15 and I didn’t get it how.

1 Like

If you carefully watch each variable as you go through the loop, you will see they change like this:

cnt = 3
a=3
b=3
c=1

cnt = 4
a=5
b=5
c=3

cnt = 5
a=9
b=9
c=5

cnt = 6
a=15
b=15
c=9

The reason the last iteration runs is because before the while loop was entered, cnt was 5, thereby satisfying the condition cnt < 6

2 Likes

I totally got it. Thank you so much :slight_smile: