← Back to Blog

Tuple Mini Project — Temperature Converter

What you're building

Rebuilding, actually, one of your first projects. The temperature converter. The user picks a conversion mode and enters a temperature. The program looks up the formula from a tuple and returns the result.

What you need to know first

  • Tuples — creating and unpacking
  • Dicts — lookup by key
  • input() and float()
  • if / else
  • in to check if a key exists

The brief

Build a program that:

  • Stores each conversion as a tuple — label, formula, unit symbols
  • Shows available modes: 1 — C to F, 2 — F to C, 3 — C to K
  • Asks the user to pick a mode
  • Asks for a temperature value
  • Applies the formula and prints the result
  • Handles an invalid mode gracefully

Think before you code

  • What does each tuple need to contain to make the conversion work?
  • How do you apply a formula stored in a tuple?
  • How do you display the input unit and the output unit from the same tuple?

Your starting point

conversions = {
    "1": ("Celsius to Fahrenheit", "°C", "°F", lambda c: c * 9/5 + 32),
    "2": ("Fahrenheit to Celsius", "°F", "°C", lambda f: (f - 32) * 5/9),
    "3": ("Celsius to Kelvin",     "°C", "K",  lambda c: c + 273.15)
}

print("1 — Celsius to Fahrenheit")
print("2 — Fahrenheit to Celsius")
print("3 — Celsius to Kelvin")
mode = input("Mode: ")

# your code here

Expected output

1 — Celsius to Fahrenheit
2 — Fahrenheit to Celsius
3 — Celsius to Kelvin
Mode: 1
Temperature (°C): 100

100.0°C = 212.0°F
Mode: 5
Invalid mode.

Heads up!

  • Each tuple has four elements — unpack all four when you look it up
  • lambda is just a short way to write a function inline — call it like any function: formula(value)
  • Use :.1f to format the output to one decimal place
  • Check with in before unpacking — invalid mode means the key doesn't exist

The solution

conversions = {
    "1": ("Celsius to Fahrenheit", "°C", "°F", lambda c: c * 9/5 + 32),
    "2": ("Fahrenheit to Celsius", "°F", "°C", lambda f: (f - 32) * 5/9),
    "3": ("Celsius to Kelvin",     "°C", "K",  lambda c: c + 273.15)
}

print("1 — Celsius to Fahrenheit")
print("2 — Fahrenheit to Celsius")
print("3 — Celsius to Kelvin")
mode = input("Mode: ")

if mode in conversions:
    label, from_unit, to_unit, formula = conversions[mode]
    temp = float(input(f"Temperature ({from_unit}): "))
    result = formula(temp)
    print(f"\n{temp}{from_unit} = {result:.1f}{to_unit}")
else:
    print("Invalid mode.")
[ login to bookmark ] // copied! 18 views · 2 min
// resources
Exercise temperature_converter_2.py
← prev Using Tuples in Your Code next → The Set: Unique Values, Nothing More
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.