Loop Variables – Advanced Example
Another example: simulate a zombie
apocalypse. Every day, each zombie finds
and bites a human, turning them into a
zombie.
If we start with just one zombie, how long
does it take for the whole world (7.5 billion
people) to turn into zombies?
Loop control variable: # of zombies
Start value: 1
Continuing condition: while the number of
zombies is less than the population
Update action: double the number of
zombies every day
We use a separate variable to count the
number of days passed, as that's our output.
zombieCount = 1
population = 7.5 * 10**9
daysPassed = 0
while zombieCount < population:
daysPassed = daysPassed + 1
zombieCount = zombieCount * 2
print(daysPassed)
16