9.4k views
0 votes
For this assignment, we are building on the previous assignment and developing a function that we will use for our final project. Add this function to your code from assignment 1 and update the 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. Hand in only one program, please. Be sure to update the docstring to include the functionality that you are adding in this assignment.

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 follows:

- 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. If the user enters an invalid units value (something other tn 0, 1,or 2 be sure to tell the user and then end the program. In the real world, you would also want to figure out 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 the function must only 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, the main() function will handle all input/output. 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 asks the user to input the temperature in Celsius, and then the program 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.
Again, the requirements:

You 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 the calling function 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 in the modules.

Can someone please help me its due on april 23. and the code needs to be in python.

User SMSM
by
8.3k points

1 Answer

4 votes

Answer:

def convert_units(celsius_value, units):

if units == 0:

return celsius_value

elif units == 1:

return celsius_value * 9/5 + 32

elif units == 2:

return celsius_value + 273.15

else:

return -1 # error code for invalid units input

if __name__ == "__main__":

celsius = float(input("Enter temperature in Celsius: "))

unit = int(input("Enter desired unit of conversion (0 for Celsius, 1 for Fahrenheit, 2 for Kelvin): "))

converted_temp = convert_units(celsius, unit)

if converted_temp == -1:

print("Invalid units input. Please enter 0, 1, or 2.")

else:

print(f"The temperature in {['Celsius', 'Fahrenheit', 'Kelvin'][unit]} is {converted_temp:.2f}.")

Step-by-step explanation:

In the code, we define the convert_units function that takes in a Celsius temperature value (celsius_value) and an integer representing the desired unit of conversion (units). We use a conditional statement to check which conversion is requested and return the temperature value in that unit. If an invalid unit is provided, we return -1 as an error code.

In the if __name__ == "__main__" block, we prompt the user to enter the temperature in Celsius and the desired unit of conversion. We then call the convert_units function with these inputs and store the result in converted_temp. We check if the result is -1 (i.e. an invalid unit was provided) and notify the user of the error. Otherwise, we use an f-string to print the converted temperature with two decimal places and the corresponding unit.

Here are four sample runs of the program:

Enter temperature in Celsius: 25

Enter desired unit of conversion (0 for Celsius, 1 for Fahrenheit, 2 for Kelvin): 1

The temperature in Fahrenheit is 77.00.

User GMS
by
8.2k points