The purpose of today’s lab is to practice using selection. We will also make use of the random module. There are three components:

Begin by creating a new folder for this week's lab called lab04.

Wind Chill

Determining how cold it feels outside is not the same as just knowing the current temperature. In this exercise we will use the following National Weather Service formula to determine the wind-chill temperature, which uses just the current temperature and windspeed.

`t_(wc) = 35.74 + 0.6215t_a - 35.75v^0.16 + 0.4275t_av^0.16`

Of course, other factors, such as relative humidity and amount of cloud cover, play important roles in determining how many layers you will want to put on before venturing outside, but we’ll satisfy ourselves with temperature and wind speed.

Exercise 4.1a

Create a new file called wind_chill.py and implement the formula given above.

  1. Prompt for and read from the user the temperature in degrees Fahrenheit (called ta in the formula) and the wind speed (called v in the formula) measured in miles per hour.
  2. Compute the wind-chill temperature (called twc in the formula)
  3. Print the result

At this point you should have a program that will compute the wind-chill temperature. For sample values, see NOAA’s Wind Chill Chart. However, the formula given above cannot be used if the wind speed is below 2 mph or if the temperature is below -58 or above 41 degrees Fahrenheit.

Exercise 4.1b

Update your wind chill algorithm to validate its input as follows.

  1. Prompt for and read from the user the temperature in degrees Fahrenheit (called ta in the formula) and the wind speed (called v in the formula) measured in miles per hour.
  2. If the wind speed is less than 2 OR the temperature is less than -58 OR the temperature is greater than 41:
    1. Print a message saying the program will not work.
    Otherwise
    1. Compute the wind-chill temperature (called twc in the formula)
    2. Print the result

Modify the code to implement this updated algorithm.

We now have a program that will only do the computation when it is appropriate, but that still doesn’t help us know how many layers to put on when we want to head outside. Let's fix this!

Exercise 4.1c
Update your wind_chill.py program to print out both the wind-chill temperature and an indication of how many layers to wear. Use the following cutoffs:

Zeller’s Congruence

Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is:

`h = (q + |__((m+1)26)/10__| + K + |__K/4__| + |__J/4__| + 5J) mod 7`
where

While we could certainly use the math module to compute the floor values, we can instead use integer division to give us the same result (for positive values, as ours will be).

Exercise 4.2
Write a program called zeller.py to compute the day of the week given a year, a month and a day of the month.
  1. Prompt for and read from the user values for the year, the month and the day of the month. Each of these values should be stored as integers.
  2. If the user entered 1 or 2 for the month it needs to be converted to 13 or 14 appropriately, and the year should decrease by 1.
  3. Compute h using the formula above.
  4. Create a list of the days of the week. Start the list with Saturday, then Sunday, etc., ending with Friday
  5. Print the day of the week using h as an index for the days of the week list.

Rock, Paper, Scissors

A popular kids game, and effective way to make a choice when two people are advocating for alternatives is to use Rock, Paper, Scissors. The game is played by two people who, at a predetermined point (often on the count of three) make hand motions indicating a rock (a fist), paper (a flat hand) or scissors (2 fingers up). Each pairing has an associated winner:

In this exercise, you will create a program to allow you to play Rock, Paper, Scissors against the computer (who chooses its implement randomly each time). To create this random behavior we will use the random module.

Exercise 4.3
In this implementation we will use the integers 0, 1 and 2 to represent rock, paper and scissors respectively, Create a new file called game.py and follow this algorithm, which assumes that the user correctly enters a 0, 1 or 2.
  1. Prompt for and read user_choice, an integer representing the user’s choice. Note: The user should understand which numbers to enter for each option, so make sure your prompt is clear.
  2. Compute the computer’s choice:
    1. Generate computer_choice, a random integer value between 0 and 2.
      Note that the randint(a,b) function from the random module generates a random number between a and b inclusively. To use the random module, be sure to add an appropriate import statement at the top of your file.
  3. Determine who wins:
    1. If user_choice is 0 then
      1. if computer_choice is 1
        1. Print “Computer wins!”.
      2. Otherwise if computer_choice is 2
        1. Print “You win!”.
      3. Otherwise
        1. Print “It is a draw!”.
    2. Otherwise
      1. Fill in the necessary commands for the rest of the comparisons.

As a challenge, try to implement a shorter algorithm. There is at least one that uses the modulus operator to compare the user and computer moves.

Checking In

We will grade these exercises according to the following criteria:

Submit your solutions to these lab exercises using your appropriate submission environment.

If you worked with a partner, make sure you both have a copy of the files.

If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!