When does the While loop end in python?

can anyone explain me while loop code in python?

read_five_times = "No"
count = 0
while read_five_times == "No":
    print ("Reading workshop..")
    count = count + 1
    if(count == 5):
        read_five_times = "Yes"
print ("Good Job ! You understood the workshop.")

Hi,

The code that you shared is pretty straight forward.

Could you tell me what part of the code you understood and which part you didn’t understand, so that I can accordingly help you out?

where is the ending of while loop?

Hi @mahadikvedant2017,

The ending of the while loop, is when start of the next code line starts at the same column as the while loop.

For this code, the line

print ("Good Job ! You understood the workshop.")

Is the first line outside the while loop.

All of this is mentioned in the “Indentation of code” sub-topic under the “python programming language” topic.

Here is the link for the same
https://trainings.boltiot.com/courses/429176/lectures/6656474

1 Like

Here while loop starts in the 3rd line ends in the 7th line(indentation is must). It will repeat 4,5,6,7,lines if condition read_five_times is NO. In every successive while loop it is incrementing count variable by 1 and checking if count is 5, if it is 5, the variable read_five_times is changed to yes, so the while loop breaks.
And the last line is printed.

1 Like

Indentation clearly explains the start and end

1 Like

In the above code, before while loop, value of read_five_times is ‘No’ and count is 0. Now the control is transferred to while loop. The condition of while loop is while read_five_times is ‘No’, so the loop will continue till above condition is true. So, print statement will be executed and count will be incremented and count will become 1, then if condition will be evaluated but it is false as value of count is != 5. Now next iteration will take place and again print statement will be executed then count = 2, Now, also if condition is false. Like this, loop continues and when value of count = 5, if condition will be evaluated and value of read_five_times will become ‘Yes’. Now, again condition of while will be tested but now condition of while is not true. Hence, while loop will not be executed and control transfers to print statement outside the while loop and the statement “Good Job ! You understood the workshop.” will be printed.