Skip to content

Temperature Conversion

Temperature Conversion

Project Goals

This programming project invites you to combine what you learned about the basics of Python programming to implement a useful program that converts a temperature value between two different measurement scales, Celsius and Fahrenheit. The program inputs the temperature reading that it should convert and a configuration explaining whether it should convert from Celsius to Fahrenheit or from Fahrenheit to Celsius. Using these inputs the program performs the conversion and outputs it in the terminal. Along with adding documentation to the provided source code, you will create your own Python functions that uses both assignment statements and conditional logic to implement a correct program that passes the test suite and all of the checks. As you enhance your technical skills, you will program with tools such as VS Code and a terminal window and the Python programming language and the Poetry package manager.

Project Access

If you are a student enrolled in a Computer Science class at Allegheny College, you can access this assignment by clicking the link provided to you in Discord. Once you click this link it will create a GitHub repository that you can clone to your computer by following the general-purpose instructions in the description of the technical skills. Specifically, you will need to use the git clone command to download the project from GitHub to your computer. Now you are ready to add source code and documentation to the project!

Expected Output

This project invites you to implement a number comparison program called converter. The program accepts through its command-line a file that contains integer values encoded as text. If you run the program with the command poetry run converter --from-unit Celsius --to-unit Fahrenheit --temperature 22 it produces this output:

🧮 Converting from Celsius to Fahrenheit!

22.00 degrees in Celsius is 71.60 degrees in Fahrenheit

Since the program can also convert from Fahrenheit to Celsius, you can also run it with the command poetry run converter --from-unit Fahrenheit --to-unit Celsius --temperature 71.6 and see that it produces the following output:

🧮 Converting from Fahrenheit to Celsius!

71.60 degrees in Fahrenheit is 22.00 degrees in Celsius

One way in which you can tell that converter is working correctly is that, when given "inverse numbers", the output shows that it converts correctly in both "directions". For instance, converting 22 degrees Celsius to Fahrenheit yields 71.6 degrees and converting 71.6 degrees Fahrenheit to Celsius results in 22 degrees! To learn more about how to run this program, you can type the command poetry run converter --help to see the following output showing how to use converter:

Usage: converter [OPTIONS]

  Convert units.from Fahrenheit to Celsius or from Celsius to
  Fahrenheit.

Options:
  --from-unit [Celsius|Fahrenheit]
                                  [default: Celsius]
  --to-unit [Celsius|Fahrenheit]  [default: Fahrenheit]
  --temperature FLOAT RANGE       [default: 98.6]
  --install-completion            Install completion for the current
                                  shell.

  --show-completion               Show completion for the current shell,
                                  to copy it or customize the
                                  installation.

  --help                          Show this message and exit.

Please note that the provided source code does not contain all of the functionality to produce this output. As explained in the next section, you are invited to add all of the missing features to ensure that converter produces the expected output. Once you finish the program, it should produce all of the expected output described in this section.

Note

Recall that if you want to run the converter program you must use your terminal window to first go into the GitHub repository containing this project and then go into the converter directory that contains the project's source code. Remember that before running the program you must run poetry install to add the dependencies!

Adding Functionality

If you study the file called converter/converter/main.py you will see that it has many TODO markers that designate the parts of the program that you need to implement before converter will produce correct output. Along with adding requested source code to the main module, you should implement the following functions in the convert module:

  • def convert_celsius_to_fahrenheit(temperature: float) -> float
  • def convert_fahrenheit_to_celsius(temperature: float) -> float
  • def convert_temperature(temperature: float, from_unit: units.TemperatureUnitOfMeasurement, to_unit: units.TemperatureUnitOfMeasurement)

The first two functions in this listing input a float value that respectively represents a temperature in Celsius or Fahrenheit and then converts it to a float representing a respective temperature in Fahrenheit or Celsius. Finally, the converted_temperature function uses source code as in the following segment to first determine what type of temperature conversion the user requested and then call the appropriate function. For instance, lines 3 and 4 show that, when the requested temperature conversion is from Celsius to Fahrenheit, the converted_temperature function will call the convert_celsius_to_fahrenheit function. Alternatively, lines 6 through 7 show that convert_temperature calls convert_fahrenheit_to_celsius when a person requests temperature conversion in the opposite direction.

1
2
3
4
5
6
7
8
converted_temperature = 0
# the requested temperature conversion is Celsius --> Fahrenheit
if from_unit.value == "Celsius" and to_unit.value == "Fahrenheit":
    converted_temperature = convert_celsius_to_fahrenheit(temperature)
# the requested temperature conversion is Fahrenheit --> Celsius
elif from_unit.value == "Fahrenheit" and to_unit.value == "Celsius":
    converted_temperature = convert_fahrenheit_to_celsius(temperature)
return converted_temperature

Once you have correctly resolved all of the TODO markers in converter, it should produce the expected output described in the previous section. You can use the following equations to guide your implementation of the convert_fahrenheit_to_celsius and convert_celsius_to_fahrenheit functions. Knowing that these equations use \(C\) and \(F\) to respectively denote the temperature in Celsius and Fahrenheit, you can translate them into Python source code that correctly performs temperature conversion.

\[ C = (F-32) \times \frac{5}{9} \]
\[ F = (C \times \frac{9}{5}) + 32 \]

Running Checks

If you study the source code in the pyproject.toml file you will see that it includes the following section that specifies different executable tasks like lint. If you are in the converter directory that contains the pyproject.toml file and the poetry.lock file, the tasks in this section make it easy to run commands like poetry run task lint to automatically run all of the linters designed to check the Python source code in your program and its test suite. You can also use the command poetry run task black to confirm that your source code adheres to the industry-standard format defined by the black tool. If it does not adhere to the standard then you can run the command poetry run task fixformat and it will automatically reformat the source code.

Along with running tasks like poetry run task lint, you can leverage the relevant instructions in the technical skills to run the command gatorgrade --config config/gatorgrade.yml to check your work. If your work meets the baseline requirements and adheres to the best practices that proactive programmers adopt you will see that all the checks pass when you run gatorgrade. You can study the config/gatorgrade.yml file in your repository to learn how the GatorGrade program runs GatorGrader to automatically check your program and technical writing.

If your program has all of the anticipated functionality, you can run the command poetry run task test and see that the test suite produces output like this:

tests/test_convert.py ........

============================ 8 passed in 0.02s =============================

The test_convert test suite contains test cases for all of the functions mentioned in the previous section. Even if the test cases for convert_fahrenheit_to_celsius and convert_celsius_to_fahrenheit pass as expected it is possible that those for convert_temperature make not if the way in which it calls the specific temperature conversion functions is not correct. When one or more test cases fail, make sure you check to see which ones are failing so that you can better know where to start the debugging process! The following test case shows how to test convert_temperature when lines 3 and 4 configure convert_temperature to convert from Celsius to Fahrenheit. After setting the input temperature to 0 on line 1 and calling the convert_temperature function on line 5, the test case checks on line 6 that the conversion function produced the temperature value of 32, failing the test if that is not the case.

1
2
3
4
5
6
def test_convert_celsius_to_fahrenheit_wrapper():
    temperature = 0
    from_unit = units.TemperatureUnitOfMeasurement.celsius
    to_unit = units.TemperatureUnitOfMeasurement.fahrenheit
    converted_temperature = convert.convert_temperature(temperature, from_unit, to_unit)
    assert converted_temperature == 32
Note

Don't forget that when you commit source code or technical writing to your GitHub repository for this project, it will trigger the run of a GitHub Actions workflow. If you are a student at Allegheny College, then running this workflow consumes build minutes for the course's organization! As such, you should only commit to your repository once you have made substantive changes to your project and you are ready to confirm its correctness. Before you commit to your repository, you can still run checks on your own computer by either using Poetry and GatorGrader.

Project Reflection

Once you have finished all of the previous technical tasks, you can use a text editor to answer all of the questions in the writing/reflection.md file. For instance, you should provide the output of the Python program in a fenced code block and explain the meaning of the Python source code segments that you implemented and tested. Along with answering all the questions about your experiences with project, you should explain every function call that occurs when running the program with a command like poetry run converter --from-unit Fahrenheit --to-unit Celsius --temperature 71.6.

Project Assessment

Since this is a programming project, it is aligned with the applying and analyzing levels of Bloom's taxonomy. You can learn more about how a proactive programming expert will assess your work by examining the assessment strategy. From the start to the end of this project you may make an unlimited number of reattempts at submitting source code and technical writing that meet all aspects of the project's specification.

Note

Before you finish all of the required deliverables required by this project is worth pausing to remember that the instructor will give advance feedback to any learner who requests it through GitHub and Discord at least 24 hours before the project's due date! Seriously, did you catch that? This policy means that you can have a thorough understanding of ways to improve your project before its final assessment! To learn more about this opportunity, please read the assessment strategy for this site.

Seeking Assistance

Emerging proactive programmers who have questions about this project are invited to ask them in either the GitHub discussions forum or the Proactive Programmers Discord server. Before you ask your question, please read the advice concerning how to best participate in the Proactive Programmers community. If you find a mistake in this project, please describe it and propose a solution by creating an issue in the GitHub Issue Tracker.


Updated: 2023-03-24   Created: 2021-08-12
Create an issue with feedback about "Temperature Conversion"
Check out all the exciting topics covered on this site