The main topics on this worksheets are variables, the built-in types int, float, and string, and the use of booleans and conditionals.
Collaboration on worksheets is strongly encouraged.
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.)
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.
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.
# The text shown below could be the content of `heatindex.py`
# MCS 260 Worksheet 2 problem 1: Heat index
# Emily Dumas
# Read input
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
# into the formula instead.
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)
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
There are several ways to write the conditional logic. We present three options. The first is the cleanest.
# The text shown below could be the content `intlike.py`
# MCS 260 Worksheet 2 problem 2: Number preferences
# Solution 1
# Emily Dumas
# Read input
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")
# The text shown below could be the content `intlike.py`
# MCS 260 Worksheet 2 problem 2: Number preferences
# Solution 2
# Emily Dumas
# Read input
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")
# The text shown below could be the content `intlike.py`
# MCS 260 Worksheet 2 problem 2: Number preferences
# Solution 3
# Emily Dumas
# Read input
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")
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.
Enter numeric temperature: 91
Which scale is tbhat measured in? (C=celsius, F=Fahrenheit, K=Kelvin) F
The corresponding Kelvin temperature is 305.9277777777778K
Enter numeric temperature: 40
Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) C
The corresponding Kelvin temperature is 313.15K
Enter numeric temperature: 500
Which scale is that measured in? (C=celsius, F=Fahrenheit, K=Kelvin) K
The corresponding Kelvin temperature is 500.0K
# The text shown below could be the content `kelvinator.py`
# MCS 260 Worksheet 2 problem 3: Temperature converter
# Emily Dumas
# Read input
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="")
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.