Loading lessons...
Project 2: Temperature Converter
Project 2: Temperature Converter
Convert Celsius to Fahrenheit (and back) with a clean menu.
The goal
Ask the user which direction to convert and a temperature, then print the result rounded to one decimal.
What you practice
- if / elif / else branches
- float conversion
- Math:
(c 9) / 5 + 32and(f - 32) 5 / 9 round()
Starter code
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
choice = input("Choose 1 or 2: ")
if choice == "1":
c = float(input("Celsius: "))
f = (c * 9) / 5 + 32
print(f"{c} C is {f:.1f} F")
elif choice == "2":
f = float(input("Fahrenheit: "))
c = (f - 32) * 5 / 9
print(f"{f} F is {c:.1f} C")
else:
print("Unknown choice")
Step-by-step
- Run the starter and test both conversions with
0and32. - Add a guard that rejects choices that aren't "1" or "2".
- Wrap the float conversion in a try/except.
- Re-run with nonsense input to confirm it doesn't crash.
Checklist
- [ ] Both conversion formulas are used
- [ ] The choice branch works
- [ ] Results print with one decimal
- [ ] Bad input is handled
- [ ] The program runs without errors
TL;DR
- Branch with if/elif/else.
- Convert strings with float().
:.1fformats to one decimal.