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

# MCS 260 Fall 2021 Worksheet 2 Solutions¶

• Course instructor: David 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.

### Solution¶

In [1]:
# The text shown below could be the content of heatindex.py
# MCS 260 Worksheet 2 problem 1: Heat index
# David Dumas

T = float(input("Enter temperature (degrees F): "))
R = float(input("Enter relative humidity (percent): "))

# Store coefficients in variables to make heat index formula
# less bulky.  This is optional; you could put the numbers directly
c0 = -42.379
c1 = 2.04901523
c2 = 10.14333127
c3 = -0.22475541
c4 = -0.00683783
c5 = -0.05481717
c6 = 0.00122874
c7 = 0.00085282
c8 = -0.00000199

# Compute the heat index
H = c0 + c1*T + c2*R + c3*T*R + c4*T*T + c5*R*R + c6*T*T*R + c7*T*R*R + c8*T*T*R*R

print("The heat index is:",H)

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


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

### Solution¶

There are several ways to write the conditional logic. We present three options. The first is the cleanest.

In [4]:
# The text shown below could be the content intlike.py
# MCS 260 Worksheet 2 problem 2: Number preferences
# Solution 1
# David Dumas

n = int(input())

# Classify and print result
if n%10 == 0:  # multiple of 10 is equivalent to multiple of 5 and of 2!
print("I LOVE IT")
elif n%2 == 0 or n%5 == 0:
print("I like it")
else:
print("It's ok")

260
I LOVE IT

In [5]:
# The text shown below could be the content intlike.py
# MCS 260 Worksheet 2 problem 2: Number preferences
# Solution 2
# David Dumas

n = int(input())

# Classify and print result
if n%2 == 0 and n%5 == 0:
print("I LOVE IT")
elif n%2 == 0 or n%5 == 0:
print("I like it")
else:
print("It's ok")

260
I LOVE IT

In [3]:
# The text shown below could be the content intlike.py
# MCS 260 Worksheet 2 problem 2: Number preferences
# Solution 3
# David Dumas

n = int(input())

# Classify and print result
if n%2 == 0:
# It's even, but it might also be a multiple of 5
if n%5 == 0:
print("I LOVE IT")
else:
print("I like it")  # multiple of 2, not 5
elif n%5 == 0:
print("I like it")      # multiple of 5, not 2
else:
print("It's ok")

260
I LOVE IT


## 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 tbhat 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

### Solution¶

In [7]:
# The text shown below could be the content kelvinator.py
# MCS 260 Worksheet 2 problem 3: Temperature converter
# David Dumas

T = float(input("Enter numeric temperature: "))
scale = input("Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) ")

# Apply one of the conversion formulas depending on the
# scale indicated by the user.  Store Kelvin temperature
# in new variable TK
if scale == "C":
# Celsius
TK = T + 273.15
elif scale == "F":
# Fahrenheit
TK = 0.5555555555555555555 * (T + 459.67)
else:
# Kelvin (no conversion needed)
TK = T

print("The corresponding Kelvin temperature is ",TK,"K",sep="")

Enter numeric temperature: 90
Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) F
The corresponding Kelvin temperature is 305.37222222222226K


### Solution remarks¶

This solution does what was asked while minimizing complexity, but it doesn't do any input validation. If you enter "walrus" as the temperature scale, it will be interpreted as Kelvin because that case is in the "else" clause of the conditional.

A better way would be to check for the known scales ("C", "F", "K") and then have an else that reports an error and exits. However, we haven't talked about the cleanest way to exit a program early yet.

## Revision history¶

• 2021-09-02 Initial release