The purpose of this mini-lab is to practice working with while loops and for loops. It will also introduce the use of random numbers.
Editor
in Spyder, create a new file for the functions you will
write in
this mini-lab. Then save the new file with a name
representative of this minilab.
In[1]: guessingGame() Enter a number between 1 and 10: 7 Your guess was too large. Enter a number between 1 and 10: 2 Your guess was too small. Enter a number between 1 and 10: 4 Congratulations you found it! 4 was the number!
Stop and Think: What type of loop structure would be appropriate to use in this function, given that you don't know how many times the program must execute this set of statements?
To generate random numbers, we will use the random
module from Python's standard library. To use this module in
your
program, you must add the following statement at the top of your
program:
import randomFrom the
random
module, we will use the
randint
function. This function
takes two parameters
that represent the range for which we want our
random number and
returns an integer within that range. For
example, calling the randint
function as in
random.randint(1,5)
will give
us some number
in the range of 1 to 5 (with 1 and 5
included in the range).
(See Section 5.7 of your text for more
details of generating random numbers.)
In[1]: predictPopulation() Starting number of organisms: 2 Average daily increase: 0.30 Number of days to multiply: 10 Day Approximate Population 1 2 2 2.6 3 3.38 4 4.394 5 5.7122 6 7.42586 7 9.653619 8 12.5497 9 16.31462 10 21.209
Stop and Think: What are good values to test? How do you know what the approximate population values should be? Should you calculate some of these by hand to make sure your program is working correctly?