Looping Until a Condition Is Reached

for loops are useful only if you know how many iterations of the loop you need. In some situations, it is not known in advance how many loop iterations to execute. In a game program, for example, you can’t know whether a player is going to want to play again or quit. In these situations, we use a while loop. The general form of a while loop is as follows:

 while​ expression:
  block

The while loop expression is sometimes called the loop condition and it is similar to condition of an if statement. When Python executes a while loop, it evaluates the expression. If that expression evaluates to False, that is the end of the execution of the loop. If the expression evaluates to True, on the other hand, Python executes the loop body once and then goes back to the top of the loop and reevaluates the expression. If it still evaluates to True, the loop body is executed again. This is repeated—expression, body, expression, body—until the expression evaluates to False, at which point Python stops executing the loop.

Here’s an example:

 >>>​​ ​​rabbits​​ ​​=​​ ​​3
 >>>​​ ​​while​​ ​​rabbits​​ ​​>​​ ​​0:
 ...​​ ​​print(rabbits)
 ...​​ ​​rabbits​​ ​​=​​ ​​rabbits​​ ​​-​​ ​​1
 ...
 3
 2
 1

Notice that this loop did not print 0. When the number of rabbits reaches zero, the loop expression evaluates to False, so the body isn’t executed. Here’s a flowchart for this code:

images/loop/while.png

As a more useful example, we can calculate the growth of a bacterial colony using a simple exponential growth model, which is essentially a calculation of compound interest:

P(t + 1) = P(t) + rP(t)

In this formula, P(t) is the population size at time t and r is the growth rate. Using this program, let’s see how long it takes the bacteria to double their numbers:

 time = 0
 population = 1000 ​# 1000 bacteria to start with
 growth_rate = 0.21 ​# 21% growth per minute
 while​ population < 2000:
  population = population + growth_rate * population
 print​(round(population))
  time = time + 1
 
 print​(​"It took"​, time, ​"minutes for the bacteria to double."​)
 print​(​"The final population was"​, round(population), ​"bacteria."​)

Because variable time was updated in the loop body, its value after the loop was the time of the last iteration, which is exactly what we want. Running this program gives us the answer we were looking for:

 1210
 1464
 1772
 2144
 It took 4 minutes for the bacteria to double.
 The final population was 2144 bacteria.

Infinite Loops

The preceding example used population < 2000 as a loop condition so that the loop stopped when the population reached double its initial size or more. What would happen if we stopped only when the population was exactly double its initial size?

 # Use multivalued assignment to set up controls
 time, population, growth_rate = 0, 1000, 0.21
 
 # Don't stop until we're exactly double the original size
 while​ population != 2000:
  population = population + growth_rate * population
 print​(round(population))
  time = time + 1
 
 print​(​"It took"​, time, ​"minutes for the bacteria to double."​)

Here is this program’s output:

 1210
 1464
 1772
 2144
 ...​​3,680​​ ​​lines​​ ​​or​​ ​​so​​ ​​later...
 inf
 inf
 inf
 ...​​and​​ ​​so​​ ​​on​​ ​​forever...

Whoops—since the population is never exactly two thousand bacteria, the loop never stops. The first set of dots represents more than three thousand values, each 21 percent larger than the one before. Eventually, these values are too large for the computer to represent, so it displays inf (or on some computers 1.#INF), which is its way of saying “effectively infinity.”

A loop like this one is called an infinite loop, because the computer will execute it forever (or until you kill your program, whichever comes first). In IDLE, you kill your program by selecting Restart Shell from the Shell menu; from the command-line shell, you can kill it by pressing Ctrl-C. Infinite loops are a common kind of bug; the usual symptoms include printing the same value over and over again or hanging (doing nothing at all).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216