Description
For this assignment, we are building on the previous assignment and developing convert_units(), a function that we will use in our final project. Add this function to your code from assignment 1 and update the Module docstring.
Make a copy of assignment_1 and name it assignment_2. When updating the docstring be sure to add a short description of this assignment just above the description of assignment 1. Submit only one program, please.
Here’s my example of the docstring
“””
Assignment Two: Temperature Conversions
Submitted by Mike Murphy
Submitted: January 22, 2023
Assignment 2: This assignment adds code to prompt the user for a temperature in Celsius and
then converts that temperature to a specified different temperature unit.
Assignment 1: This program demonstrates printing lines of text to the screen
“””
Temperature Conversions
Implement the function:
convert_units(celsius_value, units)
that accepts two arguments, a float (celsius_value) and an int (units).
The function will return the supplied temperature, converted as specified to a float:
– If units = 0, return temperature in Celsius (no change)
– if units = 1, return temperature in Fahrenheit
– if units = 2, return temperature in Kelvin
Prompt the user for the Celsius temperature and units in main(), the unit-test code. Validate units in convert_units, if the user enters an invalid units value (something other than 0, 1,or 2 convert_units must return an error code to main() and main() will tell the user and then end the program. In the real world, you would also want to check for valid values for celsius_value. Don’t worry about prompting the user to reenter the units, just tell them to run the program again. When we learn about loops, we will prompt again when invalid input is presented.
Note that convert_units() must return a float (floating point number), not a string. This float is the result of the conversion done by the convert_units() function. Your convert_units() function performs the computation based on the arguments passed and the main() function will handle all of the input/output. Remember the main() function contains the unit-test code.
Formulas:
T(°F) = T(°C) × 9/5 + 32
T(°k) = T(°C) + 273.15
Unit Test
Unit testing is a testing method by which individual units of source code are tested to determine if they are ready to use.
In the main() function, add code that prompts the user to input the temperature in Celsius, and then call convert_units() to perform the conversion. main() should print the temperature converted to the temperature requested by the user, using f-strings. This code in the main function is called a “unit test” because its only purpose is to test an individual unit of your code. In this case, you want to test the function convert_units.
As happens frequently in the English language we are using the word unit in two completely different contexts. The term unit test is a test designed to test just one function or a related group of functions. The word units in converts_units if referring to units used in the measurement of temperature.
The unit test code will be removed from main() in the next assignment, it will be replaced by the unit test code for Lab Assignment 3.
Be sure to include at least four sample runs of your program! One for each of the three valid conversions and one responding to an invalid request. The validation of the input will be done by convert_unit, not main(). convert_units will be used later in the project.
Here is my 4 sample outputs:
r”””
“C:UsersMikeDocumentsPyCharmProjectsC S 3A Winter 2023venvScriptspython.exe” “C:/Users/Mike/Documents/PyCharmProjects/C S 3A Winter 2023/Assignment_2/shaikhraed_LATE_211435_6979190_AssignmentTwo-2.py”
STEM Center Temperature Project
Mike Murphy
Please enter a temperature in degrees Celsius: 45
Please enter the desired conversion (0 for no conversion, 1 to convert to Fahrenheit, 2 to Kelvin): 0
That’s 45 degrees Celsius
Here is a second sample output:
C:UsersMikeDocumentsPyCharmProjectsC S 3A Winter 2023venvScriptspython.exe” “C:/Users/Mike/Documents/PyCharmProjects/C S 3A Winter 2023/Assignment_2/shaikhraed_LATE_211435_6979190_AssignmentTwo-2.py”
STEM Center Temperature Project
Mike Murphy
Please enter a temperature in degrees Celsius: 45
Please enter the desired conversion (0 for no conversion, 1 to convert to Fahrenheit, 2 to Kelvin): 1
That’s 113.00 degrees Fahrenheit
Here is a third sample output:
“C:UsersMikeDocumentsPyCharmProjectsC S 3A Winter 2023venvScriptspython.exe” “C:/Users/Mike/Documents/PyCharmProjects/C S 3A Winter 2023/Assignment_2/shaikhraed_LATE_211435_6979190_AssignmentTwo-2.py”
STEM Center Temperature Project
Mike Murphy
Please enter a temperature in degrees Celsius: 45
Please enter the desired conversion (0 for no conversion, 1 to convert to Fahrenheit, 2 to Kelvin): 2
That’s 318.15 degrees Kelvin
Here is a fourth sample output:
“C:UsersMikeDocumentsPyCharmProjectsC S 3A Winter 2023venvScriptspython.exe” “C:/Users/Mike/Documents/PyCharmProjects/C S 3A Winter 2023/Assignment_2/shaikhraed_LATE_211435_6979190_AssignmentTwo-2.py”
STEM Center Temperature Project
Mike Murphy
Please enter a temperature in degrees Celsius: 45
Please enter the desired conversion (0 for no conversion, 1 to convert to Fahrenheit, 2 to Kelvin): 3
*** Invalid Input, the conversion type must be 0, 1, or 2): You entered an illegal value
“””
Again, the requirements:
The function signature must exactly match: convert_units(celsius_value, units)
Your convert_units function must accept unit values of 0, 1, or 2.
Your convert_units function will return an error code to the calling function (main()) for any other values and main() will notify the user of the error.
Your convert_units() function should be able to accept and convert any Celsius value
Your convert_units() function should return a float value (not a string) based on what value was passed to the units parameter
Your convert_units() function should not print anything.
You must use f-strings to incorporate numbers into the output.
Don’t forget the
if __name__ == “__main__” logic as described previously.