![]() |
![]() |
|
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
Background: We have seen
On Day 0, the virus mutated, and one person was infected. “It's only one person who wants to eat brains — how bad can it be?”, the researchers consoled themselves. But Patient Zed bit another researcher and fled. The next morning, that researcher was also infected, and now there were two Zeta-Omega Mutated Bioform Interferon Entities (which the ridiculously, in their unscientific sensationalism, shortened to "zombie").
Each day, every victim bites one more non-infected person, doubling the number of victims the next morning. Computer modeling can be very helpful, in determining the course of the zombie apocalypse.
# zombiePopulation2 : int -> int # Given a number of days, return the number of zombies at the end of that many days. # def zombiePopulation2( days ): zombiesSoFar = 1 dayNum = 0 while (dayNum < days): zombiesSoFar = zombiesSoFar * 2 dayNum = dayNum + 1 # Put this at the *end* of the body, to act like the for-loop does! # For illustration/understanding only: print( "At end of loop: dayNum=" + str(dayNum) + " and zombiesSoFar=" + str(zombiesSoFar) ) return zombiesSoFar |
What the
Consider: How many days until there are at least 10,000 infected?
Since we don't know how many days it takes, we don't know what
def daysTilExceed( popSize ): zombiesSoFar = 1 dayNum = 0 while (zombiesSoFar < popSize): zombiesSoFar = zombiesSoFar * 2 dayNum = dayNum + 1 # For illustration/understanding only: print( "At end of loop: dayNum=" + str(dayNum) + " and zombiesSoFar=" + str(zombiesSoFar) ) return dayNum |
while (condition): statements… |
i = 0 while (i != 10): print("Whoops, I won't hit my stop-condition exactly! (Hit 'stop' button at center-right.)" i = i+3 print("The value of i is: " + str(i) ) j = 0 while (j < 10): print("Whoops, I am forgetting to update my index variable! (Hit 'stop' button at center-right.)") print("The value of j is: " + str(j) ) |
YOUR TASK: (not to be checked off — but this will be on a quiz)
Re-write
Remember:
in a
1
Well, in every example we've made, the index-variable is an
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Apr.28 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |