5 total points
Directions¶
Due: 10/27/2025 @ 11:59 p.m.
Change the name of your notebook to EX8_FirstLast.ipynb, where First is your first name and Last is your last name.
For each of the following prompts, write or modify Python code that fulfills the requirements.
Creating and using classes¶
Setup for the problems¶
class Observation:
def __init__(self, high, low, dew, pres):
self.high_temperature = high
self.low_temperature = low
self.dewpoint = dew
self.pressure = pres
def F_to_K(self):
K = (self.high_temperature + 459.67) * (5 / 9)
return K
def F_to_C(self):
C = (self.high_temperature - 32) * (5 / 9)
return CIn Colab, upload this notebook and open the project manager menu (looks like three dots and three lines on the left side of the page). Then click on the folder icon. Once the runtime is connected, right click in the area under “sample_data” and select “New File” and name it my_file.py.
Copy the Observation class code above (do not copy the ``````). Double click on my_file.py in Colab and it will open a text editor on the right side of the page. Paste the Observation code into that editor.
Now, run the code below. If you set this up correctly, it should print out the following prompt:
The high temperature is 85
The low temperature is 65
The dewpoint is 64
The pressure is 29.4
High in K = 302.5944444444445
High in C = 29.444444444444446from my_file import Observation
obs = Observation(high=85, low=65, dew=64, pres=29.4)
print("The high temperature is", obs.high_temperature)
print("The low temperature is", obs.low_temperature)
print("The dewpoint is", obs.dewpoint)
print("The pressure is", obs.pressure)
print("High in K =", obs.F_to_K())
print("High in C =", obs.F_to_C())Problem 1
Add a method to Observation in the text editor for my_file.py named formatted_conversion that looks like the following code. You will need to restart your colab notebook to get the changes from the other file (Runtime -> Restart Session):
def formatted_conversion(self, conversion):
# your code here
return conversion_strThat provides the str representation of the conversion result with two decimal places using the code below:
print("The high temperature is", obs.high_temperature)
print("The low temperature is", obs.low_temperature)
print("The dewpoint is", obs.dewpoint)
print("The pressure is", obs.pressure)
print("High in K =", obs.formatted_conversion('F_to_K'))
print("High in C =", obs.formatted_conversion('F_to_C'))Problem 2
Add a new property named unit in the constructor that identifies the input unit as ‘F’, ‘C’, or ‘K’. Add the the methods C_to_F and K_to_F and update formatted_conversion to include these functions as options. If the user does not use the correct method based on unit. Provide an error message saying “incorrect unit. Expected ‘F_to_C’ or ‘F_to_K’” depending on the unit. For example, if they try the option “F_to_C”, but the unit is “K”, the user should only be able to use ‘K_to_F’ or ‘K_to_C’.
Provide cases that prove you implemented the requirements correctly.
Please include the code for the updated my_file.py in the formatted code area below (double click / click here to open up markdown):
Paste your code here