Help in python code #21492
-
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. n = 6 the answer it return is 15 and I didn’t get it how. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
If you carefully watch each variable as you go through the loop, you will see they change like this: cnt = 3 cnt = 4 cnt = 5 cnt = 6 The reason the last iteration runs is because before the while loop was entered, cnt was 5, thereby satisfying the condition cnt < 6 |
Beta Was this translation helpful? Give feedback.
-
I totally got it. Thank you so much :slight_smile: |
Beta Was this translation helpful? Give feedback.
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