Temperature Converter (Celsius to Fahrenheit)
BasicPython
Convert temperatures with a simple Python program
1) Project Overview
- This program asks the user for a temperature in Celsius and converts it to Fahrenheit using a simple math formula.
- The user types a number (like 25), and the program prints the temperature in Fahrenheit (like 77).
2) Learning Objectives
By completing this project, students will:
- Understand how to get input from a user with
input() - Convert text input into numbers using
float() - Use variables, basic arithmetic, and the conversion formula
- Format output clearly
- Handle simple input errors with try/except
- Practice writing readable code with comments
3) Step-by-Step Explanation
- Know the formula:
- Fahrenheit = (Celsius × 9/5) + 32
- Plan the program:
- Input: a number typed by the user (Celsius)
- Process: use the formula to compute Fahrenheit
- Output: show the result in a friendly sentence
- Start with a greeting:
- Print a welcome message so the user knows what the program does.
- Ask for input:
- Use
input()to get a temperature in Celsius as text.
- Use
- Convert the input:
- Change the text to a number using
float(). This lets Python do math with it.
- Change the text to a number using
- Do the math:
- Apply the Celsius-to-Fahrenheit formula.
- Show the result:
- Print the Fahrenheit value. You can round it to 2 decimal places so it looks neat.
- Handle mistakes:
- If the user types something that isn't a number, show a helpful message instead of crashing.
- Test your program:
- Try values like 0 (should give 32 F), 100 (should give 212 F), and -40 (interesting: -40 C is -40 F).
4) Complete and Well-Commented Python Code
# Temperature Converter: Celsius to Fahrenheit
# 1) Print a friendly title so the user knows what this program does.
print("Welcome to the Temperature Converter (Celsius to Fahrenheit)")
# 2) Ask the user for a temperature in Celsius.
user_input = input("Enter a temperature in Celsius: ")
# 3) Try to convert the input text into a number (float).
# If the user types something that's not a number, we catch the error.
try:
celsius = float(user_input) # turn text like "25" into the number 25.0
# 4) Apply the conversion formula: F = (C × 9/5) + 32
fahrenheit = (celsius * 9/5) + 32
# 5) Show the result.
# The :.2f means "show 2 numbers after the decimal point".
print(f"{celsius:.2f} C is equal to {fahrenheit:.2f} F")
except ValueError:
# This runs if float(user_input) fails (for example, input was "ten").
print("Oops! Please enter a number, like 25 or -4.5.")
5) Output Examples
Example 1 (valid input):
Welcome to the Temperature Converter (Celsius to Fahrenheit)
Enter a temperature in Celsius: 0
0.00 C is equal to 32.00 F
Enter a temperature in Celsius: 0
0.00 C is equal to 32.00 F
Example 2 (valid input):
Welcome to the Temperature Converter (Celsius to Fahrenheit)
Enter a temperature in Celsius: 100
100.00 C is equal to 212.00 F
Enter a temperature in Celsius: 100
100.00 C is equal to 212.00 F
Example 3 (valid input with decimals):
Welcome to the Temperature Converter (Celsius to Fahrenheit)
Enter a temperature in Celsius: -4.5
-4.50 C is equal to 23.90 F
Enter a temperature in Celsius: -4.5
-4.50 C is equal to 23.90 F
Example 4 (invalid input):
Welcome to the Temperature Converter (Celsius to Fahrenheit)
Enter a temperature in Celsius: cold
Oops! Please enter a number, like 25 or -4.5.
Enter a temperature in Celsius: cold
Oops! Please enter a number, like 25 or -4.5.
6) Small Exercise (Challenge)
Make the program repeat until the user wants to quit
- Idea: After showing the result, ask: "Convert another? (y/n):"
- If the user types y, run again; if n, say goodbye and end.
- Hint: Use a while loop to keep asking for input, and break when the user types n.
Bonus (if you want more): Add the reverse conversion (Fahrenheit to Celsius) using the formula C = (F − 32) × 5/9 and let the user choose which direction to convert.
7) Summary
Great job! You built a real, useful tool with just a few lines of Python. You learned how to read input, do calculations, handle mistakes, and show clear results. Keep experimenting—every small program you write builds your confidence and skills. You've got this!