Set up a small, useful project

A temperature converter is a good first project because its purpose is easy to understand, but it still needs more than a formula. A person might enter a number, leave the input blank, type a word, or choose an unsupported unit. Handling those cases makes the program more useful and gives you practice separating calculations from input validation.

You need a text editor and Python 3.8 or later. Save the program below as temperature_converter.py, then run it from a terminal with python temperature_converter.py. On systems where that command points to a different Python installation, use python3 temperature_converter.py instead. This version runs in the terminal and needs no additional packages. It accepts a starting temperature in Celsius or Fahrenheit and converts it to the other unit.

Decide what counts as valid input

The two conversion formulas are straightforward: Fahrenheit equals Celsius times 9 divided by 5, plus 32. Celsius equals Fahrenheit minus 32, times 5 divided by 9. The order of operations matters in the second formula: subtract 32 before multiplying. For example, 32 °F should produce 0 °C.

The program also needs rules for input. It accepts C or F as the starting unit, ignoring surrounding spaces and letter case. Python’s float function converts numeric text such as 12, -4.5, or 1e2 into a number. If that conversion fails, the program should explain the problem rather than stop with a traceback. Not every value accepted by float is suitable, though: infinity and NaN are not temperatures, so the program rejects them with math.isfinite.

Finally, a temperature cannot be below absolute zero. That limit is -273.15 °C or -459.67 °F. Checking the limit in the unit the user entered keeps the rule simple. It also lets the program accept either boundary value without relying on the result of a floating-point conversion.

Write the converter

Copy this Python program into your file. The convert function contains the rules and calculation; main handles interaction with the user. Keeping them separate makes it easier to check the conversion without repeatedly typing responses at the prompts.

import math def convert(value, unit): if unit == "C": if value < -273.15: raise ValueError("Celsius cannot be below -273.15.") return value * 9 / 5 + 32, "F" if unit == "F": if value < -459.67: raise ValueError("Fahrenheit cannot be below -459.67.") return (value - 32) * 5 / 9, "C" raise ValueError("Choose C or F.") def main(): unit = input("Convert from C or F? ").strip().upper() raw = input("Temperature: ").strip() try: value = float(raw) if not math.isfinite(value): raise ValueError("Enter a finite number.") result, target = convert(value, unit) except ValueError as error: print("Invalid input:", error) return print(f"{value:g} °{unit} = {result:.2f} °{target}") if __name__ == "__main__": main()

The try block covers both number parsing and the checks in convert. When either step raises a ValueError, the program prints a short message and ends normally. The result uses two decimal places for display, while the calculation itself is not rounded beforehand. Formatting the output is different from changing the value used in the formula.

Check ordinary values and awkward ones

Run the program several times with known conversions. Enter C and 0 to get 32.00 °F; enter F and 32 to get 0.00 °C. Try C and 100 to get 212.00 °F. The value -40 is useful because it converts to -40 in either direction. For a decimal input, try C and 37.5; the displayed result should be 99.50 °F. These checks exercise both formulas, positive and negative numbers, and output formatting.

Next, try inputs that should fail. A blank temperature or a word such as warm cannot be converted to a number. The text inf is accepted by float but rejected by the finite-number check. A unit such as K is rejected because this project supports only C and F. Values below -273.15 °C or -459.67 °F should also produce an error. For boundary checks, enter exactly -273.15 with C and exactly -459.67 with F; both are allowed.

If a result surprises you, inspect one step at a time: confirm the unit entered, check where the parentheses are in the Fahrenheit-to-Celsius formula, and compare the unrounded calculation with the displayed value. After this version works, a useful extension is to let the user make another conversion without restarting the program. Keep the same validation rules when you add the loop.

Back to category ↗