Python Note 7
Python Note 7
Python Note 7
02/01/2024
Note that this record also declares that if there is nothing to do, nothing at all
will happen.
while conditional_expression:
instruction
If you notice some similarities to the if instruction, that's quite all right.
Indeed, the syntactic difference is only one: you use the word while instead
of the word if .
Note: all the rules regarding indentation are applicable here, too. We'll show
you this soon.
while conditional_expression:
instruction_one
instruction_two
instruction_three
:
:
instruction_n
It is now important to remember that:
if you want to execute more than one statement inside one while ,
you must (as with if ) indent all the instructions in the same way;
an instruction or set of instructions executed inside the while loop is
called the loop's body;
if the condition is False (equal to zero) as early as when it is tested for
the first time, the body is not executed even once (note the analogy of
not having to do anything if there is nothing to do);
the body should be able to change the condition's value, because if the
condition is True at the beginning, the body might run continuously to
infinity - notice that doing a thing usually decreases the number of
things to do).
An infinite loop
while True:
print("I'm stuck inside a loop.")
This loop will infinitely print "I'm stuck inside a loop." on the screen.
NOTE
If you want to get the best learning experience from seeing how an infinite
loop behaves, launch IDLE, create a New File, copy-paste the above code,
save your file, and run the program. What you will see is the never-ending
sequence of "I'm stuck inside a loop." strings printed to the Python
console window. To terminate your program, just press Ctrl-C (or Ctrl-Break on
some computers). This will cause the so-called KeyboardInterrupt exception
and let your program get out of the loop. We'll talk about it later in the course.
Let's go back to the sketch of the algorithm we showed you recently. We're
going to show you how to use this newly learned loop to find the largest
number from a large set of entered data.
Analyze the program carefully. See where the loop starts (line 8). Locate the
loop's body and find out how the body is exited:
Check how this code implements the algorithm we showed you earlier.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: