Repetition Statements

 


Introduction

The purpose of this in-class activity is to gain practice working with for loops and to learn how to use the range function. It will also continue with the use of random numbers.



Coin Flipping

Coin flipping is used in many different types of scientific simulations, as well as in game programming. In this exercise, you are going to write a function that simulates flipping a coin some number of times (to be determined by the user). The function will print out "heads" or "tails" each time the coin is flipped, and will print out the total number of heads and tails at the end. The following steps will help you to write this function.

  1. Creat a new notebook in Google Colab for the functions you will write in this activity. Then save the new notebook with a name representative of this activity.
  2. In a Code cell, define a function, coinFlip, that does not take any arguments.

  3. At the beginning of this function, ask the user how many times they would like to flip the coin.

  4. Next, add a for loop statement that will iterate the number of times that the user specified. You will need to use the range function to do this. The range function returns a set of numbers to be iterated over. For example, range(3) returns 3 numbers, precisely 0, 1, and 2, whereas range(5) would give us 0, 1, 2, 3, 4.

  5. Inside the body of the for loop, you will do the following:
    • Get a random number between 0 and 1 and store it in a variable. (This is your coin flip!)
    • If that random number is 0, print "heads". Otherwise print "tails".

  6. Call this function several times to see that it is doing what you think it should.

  7. Add some counters: After you have gotten the user input, but before your loop, create two variables to keep track of the number of times you get heads/tails in the coin flip. Pick meaningful names for your variables such as numHeads and numTails. These variables should be assigned the value 0.

  8. In the if/else blocks of code, increment the counter variables by 1. There are two ways to choose from to do this:
                          num = num + 1
                          
    or
                          num += 1
                          
    where num is the name of your variable.

  9. At the end of your function, print the values of your head/tail counter variables.

  10. Call your function several times and check that it is printing the correct number of heads and tails. Calling the function might give you output that looks like the following:
            Enter the number of times to flip the coin: 3
            Heads
            Tails
            Heads
            The number of times the flip ended in heads was: 2
            The number of times the flip eneded in tails was: 1
            

  11. Once you are convinced your function is working correctly, comment out the statements that print "heads" and "tails". Have the user enter in a large value for the number of coin flips. What values should you be close to for the number of heads/tails? (Coin flipping should be roughly 50% heads/50% tails.)

Population

In this exercise, you will use a loop to display the daily population of a group of organisms based on user-input for the growth parameters. The function will ask the user to enter the starting number of organisms, the average daily population increase (as a percentage), and the number of days the organisms will be left to multiply (reproduce). For example, the output of your program may look like the following:

    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.653618
    8               12.5497034
    9               16.31461442
    10              21.20898746000002

  1. Write a function that asks the user to enter the number of organisms to start with, the daily increase (as a percentage), and the number of days to let the organisms multiply. (This means the input statements are inside the function, and this function does not need any arguments.) It then displays a table of the approximate population for each day. For each day, the population increase should be calculated and added onto the total population. The daily result should be printed.
    Hints for doing this:
    • Set up a for loop to iterate the number of times the user specified.
    • Inside the for loop, increment the number of organisms by the daily percent increase. Your calculation should look something like this:
                  numOrganisms  = numOrganisms + numOrganisms * dailyIncr
                  
    • Print out the number of the day and the number of organisms per day.
    • Add print statements as column headings.
  2. Test your function. You should run your function several times, choosing different input values each time.
  3. 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?

Submit