DEV Community

SAINAPEI LENAPUNYA
SAINAPEI LENAPUNYA

Posted on

Day 9: Temperature Converter - From Celsius to Fahrenheit - 30 Days of Python Challenge

Welcome Back to Day 9!

Hey everyone! It's Day 9 of my 30 Days of Python Challenge, and we're building another essential converter a temperature converter!

Today, we're creating a converter that switches between Celsius and Fahrenheit. Let's keep building!

Day 9: Temperature Converter - Working with Formulas

Today's mission: Build a Temperature Converter. Following yesterday's weight converter pattern, I'm creating another practical tool using conversion formulas!

What I Learned

Building this temperature converter taught me:

  • How to implement mathematical formulas in Python
  • How to use the .upper() method for case-insensitive input
  • How to work with temperature scales and their conversion formulas
  • How patterns from previous projects make new ones easier to build

The more I build, the more confident I become! 🔥

My Code

Here's what I wrote for Day 9:

# Day 9 - Temperature conversion
def c_to_f(c): return (c * 9/5) + 32
def f_to_c(f): return (f - 32) * 5/9

choice = input("Convert (C)elsius or (F)ahrenheit? ").upper()
if choice == "C":
    c = float(input("Celsius: "))
    print(f"{c} °C = {c_to_f(c):.2f} °F")
else:
    f = float(input("Fahrenheit: "))
    print(f"{f} °F = {f_to_c(f):.2f} °C")
Enter fullscreen mode Exit fullscreen mode

Breaking It Down 🔍

Let me explain each part:

  1. def c_to_f(c): return (c * 9/5) + 32 - This one-line function converts Celsius to Fahrenheit using the standard formula: °F = (°C × 9/5) + 32. Notice how we can write complex formulas right in the return statement!

  2. def f_to_c(f): return (f - 32) * 5/9 - The reverse conversion: °C = (°F - 32) × 5/9. These formulas are mathematical standards for temperature conversion.

  3. choice = input("Convert (C)elsius or (F)ahrenheit? ").upper() - Here's something new! The .upper() method converts whatever the user types to UPPERCASE. So whether they type "c", "C", or even "c", it becomes "C". This makes our program more user-friendly!

  4. if choice == "C": - If the user chose Celsius (meaning they want to convert FROM Celsius to Fahrenheit).

  5. c = float(input("Celsius: ")) - Get the temperature in Celsius.

  6. print(f"{c} °C = {c_to_f(c):.2f} °F") - Display the result with the degree symbols (°C and °F) and format to 2 decimal places. The :.2f ensures we don't get messy decimals!

  7. else: - If the user chose anything other than "C" (presumably "F"), we convert from Fahrenheit to Celsius.

  8. The else block - Same pattern but reversed!

Output

When you run this code, you'll see something like:

Convert (C)elsius or (F)ahrenheit? C
Celsius: 25
25.0 °C = 77.00 °F
Enter fullscreen mode Exit fullscreen mode

Or if you convert the other way:

Convert (C)elsius or (F)ahrenheit? F
Fahrenheit: 98.6
98.6 °F = 37.00 °C
Enter fullscreen mode Exit fullscreen mode

Temperature Conversion Formulas 🎯

Here are the formulas we're using:

Celsius to Fahrenheit:

  • Formula: °F = (°C × 9/5) + 32
  • Example: 0°C = 32°F (freezing point of water)
  • Example: 100°C = 212°F (boiling point of water)

Fahrenheit to Celsius:

  • Formula: °C = (°F - 32) × 5/9
  • Example: 32°F = 0°C
  • Example: 98.6°F = 37°C (normal body temperature)

Why This Is Useful🔗

Temperature converters are incredibly practical:

  • Travel: Understanding weather forecasts in different countries
  • Cooking: Converting recipe temperatures for your oven
  • Science: Working with international temperature standards
  • Health: Understanding body temperature readings
  • Weather: Following international news and climate data

Key Takeaways💡

  • Mathematical formulas can be directly written in Python functions
  • .upper() converts strings to uppercase for case-insensitive comparisons
  • .lower() also exists for converting to lowercase
  • Temperature conversions use specific mathematical formulas
  • The same project pattern (menu → input → calculate → display) works for many converters
  • Building similar projects reinforces your skills
  • Formatting with :.2f keeps output clean and professional

What's Next?

Tomorrow on Day 10, I'll be exploring logical operators! We'll learn how to combine multiple conditions using AND, OR, and NOT. Time to make our decision-making even more powerful! 🧠

Let's Connect!💬

I'd love to hear from you!

  • Do you prefer Celsius or Fahrenheit?
  • Have you ever been confused by temperature in a different country?
  • What other temperature scales should we add (Kelvin, maybe)?

Drop a comment below! If you're coding along, try adding Kelvin conversion to your program and share your enhanced version!

Don't forget to follow me for daily updates. Day 10 takes us deeper into logic!

*Happy Coding! *

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.