Search⌘ K
AI Features

Project: Temperature Converter

Explore how to create a Python program that converts temperatures between Celsius, Fahrenheit, and Kelvin. Learn to handle user input, apply conditional logic, organize code with functions, and validate inputs while practicing essential programming techniques in a practical project.

Create a Python program that converts temperatures between Celsius, Fahrenheit, and Kelvin. The goal is to help learners understand basic input handling, conditionals, arithmetic operations, and functions—all in a practical, real-world context.

Goals

By the end of this project, you’ll be able to:

  • Convert temperatures between Celsius, Fahrenheit, and Kelvin.

  • Use input() to get user input.

  • Apply conditionals (if, elif, else) to control program flow.

  • Use functions to organize code for reusability and clarity.

  • Handle invalid input gracefully.

Project breakdown

  • Print a welcome message, explain the conversion options, and prompt the user to choose one.

  • Get the user’s choice and the temperature to convert; handle invalid input.

  • Write a separate function for each type of temperature conversion.

  • Use if/elif/else to call the correct function and display the result.

Step 1: Program setup

  • Print a welcome message.

  • Explain the available conversion types.

  • Prompt the user to choose a conversion type (e.g., Celsius to Fahrenheit).

print("🌡️ Welcome to the Temperature Converter!")
print("Choose a conversion:")
print("1: Celsius to Fahrenheit")
print("2: Fahrenheit to Celsius")
print("3: Celsius to Kelvin")
print("4: Kelvin to Celsius")
print("5: Fahrenheit to Kelvin")
print("6: Kelvin to Fahrenheit")
Project setup

Step 2: Take the user input

  • Get the user’s choice (conversion type).

  • Ask for the temperature to convert.

  • Convert the input to a float.

choice = input("Enter your choice (1-6): ")
temp = float(input("Enter the temperature to convert: "))
Take user input

Step 3: Define conversion functions

Create separate functions for each conversion type:

def celsius_to_fahrenheit(c):
    return (c * 9/5) + 32

def fahrenheit_to_celsius(f):
    return (f - 32) * 5/9

def celsius_to_kelvin(c):
    return c + 273.15

def kelvin_to_celsius(k):
    return k - 273.15

def fahrenheit_to_kelvin(f):
    return (f - 32) * 5/9 + 273.15

def kelvin_to_fahrenheit(k):
    return (k - 273.15) * 9/5 + 32
Conversion functions

Step 4: Perform the conversion

Use conditionals to check the user’s choice and call the appropriate function:

if choice == "1":
    result = celsius_to_fahrenheit(temp)
    print("{}°C is {:.2f}°F".format(temp, result))
elif choice == "2":
    result = fahrenheit_to_celsius(temp)
    print("{}°F is {:.2f}°C".format(temp, result))
elif choice == "3":
    result = celsius_to_kelvin(temp)
    print("{}°C is {:.2f}K".format(temp, result))
elif choice == "4":
    result = kelvin_to_celsius(temp)
    print("{}K is {:.2f}°C".format(temp, result))
elif choice == "5":
    result = fahrenheit_to_kelvin(temp)
    print("{}°F is {:.2f}K".format(temp, result))
elif choice == "6":
    result = kelvin_to_fahrenheit(temp)
    print("{}K is {:.2f}°F".format(temp, result))
else:
    print("Invalid choice. Please run the program again.")
Perform conversion

Tips

  • Use while loops to allow repeated conversions without restarting the program.

  • Add input validation to avoid errors.

  • Use try/except blocks to handle non-numeric input.

  • Let users type temperature units as text (e.g., "C" to "F") instead of numbers.

  • Add ASCII thermometer graphics for fun

Mini challenge

Let the user convert multiple temperatures in one run and log the results in a list. At the end, print all conversions done during the session.

Use a while loop to let users keep converting temperatures, and store each result in a list. Exit the loop with a special input like 'q', then print the list at the end.

# Modidy the code below to let the users convert multiple temperatures in one run and log the results in a list. 
# At the end, print all conversions done during the session.
print("🌡️ Welcome to the Temperature Converter!")
print("Choose a conversion:")
print("1: Celsius to Fahrenheit")
print("2: Fahrenheit to Celsius")
print("3: Celsius to Kelvin")
print("4: Kelvin to Celsius")
print("5: Fahrenheit to Kelvin")
print("6: Kelvin to Fahrenheit")
choice = input("Enter your choice (1-6): ")
temp = float(input("Enter the temperature to convert: "))
def celsius_to_fahrenheit(c):
    return (c * 9/5) + 32

def fahrenheit_to_celsius(f):
    return (f - 32) * 5/9

def celsius_to_kelvin(c):
    return c + 273.15

def kelvin_to_celsius(k):
    return k - 273.15

def fahrenheit_to_kelvin(f):
    return (f - 32) * 5/9 + 273.15

def kelvin_to_fahrenheit(k):
    return (k - 273.15) * 9/5 + 32

Mini challenge on temperature conversion

If you’re stuck, click the “Show Solution” button.

Go further

  • Add a graphical interface using Tkinter.

  • Create a web version using Flask or Streamlit.

  • Save conversion history to a file.

You just built a useful Python utility that practices input handling, math, and logic. Keep experimenting and try personalizing it with your creative twist!