1 The Problem
We want a small program that converts a temperature from Celsius to Fahrenheit, or the other way around. The user picks which direction, types a number, and the program does the maths and shows the answer. Simple — but it teaches the core shape of every calculator-style tool: ask, calculate, show.
2 How to Think About It
Before any code, think about the two things the program needs to know and the two formulas it might use. You do not need Python yet — just the plan:
3 The Build — explained part by part
Here is the complete converter. Read each line’s note below — you should understand the whole thing from the notes alone.
# A temperature converter: Celsius to Fahrenheit and back.
print("Temperature Converter")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
choice = input("Choose 1 or 2: ")
if choice == "1":
celsius = float(input("Enter temperature in Celsius: "))
# The formula: multiply by 9/5, then add 32.
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius}\u00b0C is {fahrenheit}\u00b0F")
elif choice == "2":
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
# The reverse: subtract 32, then multiply by 5/9.
celsius = (fahrenheit - 32) * 5 / 9
print(f"{fahrenheit}\u00b0F is {celsius}\u00b0C")
else:
print("Please choose 1 or 2.")
choice = input("Choose 1 or 2: ") — read which direction they want. This comes in as text (“1” or “2”).
if choice == "1": — if they picked Celsius→Fahrenheit, do the first conversion. We compare to the text “1” because
input always gives text.celsius = float(input(...)) — read the temperature and turn it into a decimal number. We use
float (not int) because temperatures can have decimals like 36.6.fahrenheit = celsius * 9 / 5 + 32 — the formula. Python follows normal maths order: multiply and divide first, then add.
elif / else —
elif handles the reverse direction; else catches anything that is not 1 or 2 and gently asks again.
int() instead of float() — then 36.6 degrees loses its decimal and becomes 36.float() for anything that can have a decimal, like temperature.celsius * 9 / 5 + 32 but expecting celsius * (9 / 5 + 32).+ 32.choice == 1 (the number) instead of choice == "1" (the text).input() gives text, so compare to "1" with quotes.4 Test & Prove Each Part
How do we know the formulas are right? We test them against temperatures we already know — water freezes at 0°C / 32°F and boils at 100°C / 212°F. If those come out right, the formula is right.
"""test_converter.py — run: pytest -v"""
def c_to_f(celsius):
"""Convert Celsius to Fahrenheit."""
return celsius * 9 / 5 + 32
def f_to_c(fahrenheit):
"""Convert Fahrenheit to Celsius."""
return (fahrenheit - 32) * 5 / 9
def test_freezing_point():
"""0 degrees Celsius is 32 degrees Fahrenheit."""
assert c_to_f(0) == 32
def test_boiling_point():
"""100 degrees Celsius is 212 degrees Fahrenheit."""
assert c_to_f(100) == 212
def test_reverse_freezing():
"""32 degrees Fahrenheit is 0 degrees Celsius."""
assert f_to_c(32) == 0
def test_round_trip():
"""Converting there and back gives the original number."""
assert round(f_to_c(c_to_f(25)), 5) == 25
Run with pytest -v. We pulled the formulas into small functions (c_to_f, f_to_c) so the tests can check them directly. Testing against known facts — freezing and boiling points — is a powerful way to trust your maths.
5 The Interface
The program’s interface — what it asks for and what it gives back — documented plainly.
What it expects
Choose 1 or 2: 1
Enter temperature in Celsius: 25What it returns
25.0°C is 77.0°F6 Run It & Automate It
Save as converter.py and run it.
python3 converter.pyPick a direction, type a temperature, and see the conversion.
A CI tool like Jenkins runs the tests automatically whenever the code changes — every line below has a plain explanation.
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Choose 1 or 2: 1
Enter temperature in Celsius: 100
100.0°C is 212.0°FValueError: could not convert string to float25 or 36.6.// Jenkinsfile — runs the tests automatically on every change.
pipeline {
agent any // run on any available machine
stages {
stage('Get the code') {
steps { checkout scm } // download the latest code
}
stage('Install tools') {
steps {
sh 'python3 -m venv venv' // clean workspace
sh '. venv/bin/activate && pip install pytest' // the test tool
}
}
stage('Run the tests') {
steps { sh '. venv/bin/activate && pytest -v' } // check every rule
}
}
post {
success { echo 'All conversions correct.' }
failure { echo 'A conversion is wrong — look above.' }
}
}
You have a working converter. Extend it:
- Add Kelvin. Offer Celsius→Kelvin too (Kelvin = Celsius + 273.15). (Teaches: more branches.)
- Round the answer. Show
77.0°Fas a tidy number withround(value, 1). (Teaches: formatting output.) - Keep converting. Loop so the user can do many conversions without restarting. (Teaches: a while loop.)
- Guard bad input. If they type “hot”, say “please type a number” instead of crashing. (Teaches: try/except.)
float for decimals, if/elif/else for choices, and how to test maths against known facts. Related reference: Variables & Types, Control Flow.