Repetition Statements

 


Introduction

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.



Guessing Game

  1. In the 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.
     
  2. Write a function that that will allow a user to attempt to guess a secret number between 1 and 10. The function should generate a random number between 1 and 10. It should then prompt the user to make a guess. If the guess is correct, the user is congratulated and the game ends. If the guess is incorrect, the user is told whether their guess was too small or too large, and they are given another chance to guess. This process should continue until the user has guessed the correct number. The program output should look something like the following:
        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 random
    From 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.)

  3. Test your function by calling it multiple times and trying different guesses.
  4. Enhancements/Challenges: (Just for fun, not required) Expand the range of the secret number to be between 1 and 100. Then think about how you might make intelligent guesses based on the too large/too small feedback. You should be able to guess the number in 7 or fewer guesses if you do it corrrectly.

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:
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

  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 parameters.) 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.
  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