A document from MCS 260 Fall 2021, instructor Emily Dumas. You can also get the notebook file.

# MCS 260 Fall 2021 Worksheet 2¶

• Course instructor: Emily Dumas

## Topics¶

The main topics on this worksheets are variables, the built-in types int, float, and string, and the use of booleans and conditionals.

## Instructions¶

• Complete the exercises below during lab.
• Most of the exercises ask you to write Python scripts to accomplish a certain task. We recommend making a folder (with a name like "worksheet1") to store the scripts you prepare today.
• Seek assistance from your TA or from fellow students if you run into trouble.

## Collaboration¶

Collaboration on worksheets is strongly encouraged.

## Resources¶

The main course materials to refer to for this worksheet are:

(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)

## 1. Heat index¶

Hot and humid air often feels hotter than hot dry air, in part due to slower evaporation of perspiration.

For this reason it can be helpful to describe weather conditions in terms of a heat index, a number which attempts to capture the "perceived temperature" by incorporating both the air temperature and relative humidity.

There are several heat indices that are in common use, and in this problem we'll look at one of them. The formula for it is described in https://www.weather.gov/media/ffc/ta_htindx.PDF, and adapted below.

• Suppose the temperature, in degrees Fahrenheit, is $T$
• Suppose the relative humidity percentage is $R$
• Then the heat index $H$ is given by the formula $$H = c_0 + c_1 T + c_2 R + c_3 TR + c_4 T^2 + c_5 R^2 + c_6 T^2 R + c_7 T R^2 + c_8 T^2 R^2$$ where the coefficients $c_0, ..., c_8$ have the values
• $c_0 = -42.379$
• $c_1 = 2.04901523$
• $c_2 = 10.14333127$
• $c_3 = -0.22475541$
• $c_4 = -0.00683783$
• $c_5 = -0.05481717$
• $c_6 = 0.00122874$
• $c_7 = 0.00085282$
• $c_8 = -0.00000199$

Write a Python script called heatindex.py that asks the user for the temperature in degrees Fahrenheit and the relative humidity percentage (as two separate input events). The script should then print the heat index. Specifically, running python heatindex.py should result in prompts and output like the following:

Enter temperature (degrees F): 92
Enter relative humidity (percent): 60
The heat index is: 104.68441864000005

(Note that you are not supposed to write a program that uses fixed numbers like 92 and 70 for the temperature and humidity, though that would be a reasonable start. A completed program is supposed to accept whatever numbers the user types and do the calculation with those values.

## 2. Number preferences¶

Suppose you like numbers that are even (like 2, 16, and 44) and you also like numbers that are divisible by 5 (like 15, 65, and 2005). Naturally, you love numbers that are both even and divisible by 5 (like 260 and 1980).

Write a Python script intlike.py that asks the user to enter a number, and then prints a message describing how much you like that number (specifically, printing "it's ok", "I like it", "I LOVE IT").

Here are some test cases. The first line is the number you type, and the second is the expected output.

5
I like it
28
I like it
0
I LOVE IT
77810
I LOVE IT
57
It's ok
2021
It's ok

## 3. Temperature converter¶

You are probably familiar with two temperature scales: Fahrenheit (F) and Celsius (C).

There is a third temperature scale that is preferred in many scientific disciplines, the Kelvin scale (K), which has the advantage that 0K corresponds to absolute zero, the minimum temperature that is possible.

The formula to convert from Fahrenheit to Kelvin is: $$T_{Kelvin} = \frac{5}{9}(T_{Fahrenheit} + 459.67)$$ The formula to convert from Celsius to Kelvin is: $$T_{Kelvin} = T_{Celsius} + 273.15$$

Write a Python script kelvinator.py that asks the user for a numeric temperature, and which then asks the user whether the temperature was given in Celsius, Fahrenheit, or Kelvin (which the user indicates by entering C, F, or K, respectively). The script should then convert the given temperature to Kelvin and print the result.

Here are some samples of input and expected output.

• Test case 1: Asking the script to convert 91°F to Kelvin
Enter numeric temperature: 91
Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) F
The corresponding Kelvin temperature is 305.9277777777778K
• Test case 2: Asking the script to convert 40°C to Kelvin
Enter numeric temperature: 40
Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) C
The corresponding Kelvin temperature is 313.15K
• Test case 3: Asking the script to convert 500K to Kelvin
Enter numeric temperature: 500
Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) K
The corresponding Kelvin temperature is 500.0K

## 4. Bonus round¶

Here are suggestions for things to work on if you complete problems 1--3 in lab and have extra time. Solutions won't be given for these problems.

### A. Flexible scale spec¶

Make it so the temperature converter will let the user enter any one of "C", "c", "celsius", or "Celsius" to specify the Celsius scale, and similarly for the other scales.

### B. Bounds checking¶

Temperatures that are negative on the Kelvin scale do not exist. Make the temperature converter check the Kelvin temperature before printing, and make sure it is nonnegative. If it is negative, print an error message instead of the temperature, e.g. "Temperature is invalid!".

## Revision history¶

• 2021-08-28 Initial release