The purpose of today’s lab is to practice using selection. We
will also make use of the random
module. There are three
components:
lab04
.
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.
Create a new file called wind_chill.py
and implement the
formula given above.
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.
Update your wind chill algorithm to validate its input as follows.
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!
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 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).
zeller.py
to compute the day of the week given a year, a month and a day of the
month.
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.
game.py
and follow
this algorithm, which assumes that the user correctly enters a 0, 1 or
2.
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.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.
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!