DEV Community

SAINAPEI LENAPUNYA
SAINAPEI LENAPUNYA

Posted on

Day 8: Weight Converter - Building Practical Tools - 30 Days of Python Challenge

Welcome Back to Day 8!

Hey everyone! It's Day 8 of my 30 Days of Python Challenge, and we're building another practical tool a weight converter!

If you missed the previous days:

  • [Day 1: Print Statements]
  • [Day 2: Variables and Data Types]
  • [Day 3: Type Casting]
  • [Day 4: User Input]
  • [Day 5: Arithmetic Operators]
  • [Day 6: If Statements]
  • [Day 7: Simple Calculator]

Today, we're creating a converter that switches between kilograms and pounds. Let's build something useful!

Day 8: Weight Converter - Functions with Parameters

Today's mission: Build a Weight Converter. After yesterday's calculator, I'm taking functions to the next level by creating specialized converters with parameters!

What I Learned

Building this weight converter taught me:

  • How to write one-line functions for simple operations
  • How to use function parameters to pass values into functions
  • How to format numbers with specific decimal places using f-strings
  • How to create a menu-driven program with user choices

This is getting more practical every day!

My Code

Here's what I wrote for Day 8:

# Day 8 - Weight conversion (kg <-> lbs)
def kg_to_lbs(kg): return kg * 2.20462
def lbs_to_kg(lbs): return lbs / 2.20462

mode = input("Convert (1) kg->lbs or (2) lbs->kg? ")
if mode == "1":
    kg = float(input("Kg: "))
    print(f"{kg} kg = {kg_to_lbs(kg):.2f} lbs")
else:
    lbs = float(input("Lbs: "))
    print(f"{lbs} lbs = {lbs_to_kg(lbs):.2f} kg")
Enter fullscreen mode Exit fullscreen mode

Breaking It Down 🔍

Let me explain each part:

  1. def kg_to_lbs(kg): return kg * 2.20462 - This is a one-line function! Instead of using multiple lines, I put everything on one line. The function takes a parameter kg and returns the value in pounds. The conversion factor is 2.20462 (1 kg = 2.20462 lbs).

  2. def lbs_to_kg(lbs): return lbs / 2.20462 - Similar function, but converts pounds to kilograms by dividing instead of multiplying.

  3. mode = input("Convert (1) kg->lbs or (2) lbs->kg? ") - We give users a choice! This makes our program more flexible.

  4. if mode == "1": - If the user chooses option 1, we convert kg to lbs.

  5. kg = float(input("Kg: ")) - Get the weight in kilograms from the user.

  6. print(f"{kg} kg = {kg_to_lbs(kg):.2f} lbs") - This is where the magic happens! Let me break down this f-string:

    • {kg} - Shows the original weight
    • {kg_to_lbs(kg):.2f} - Calls our function AND formats it to 2 decimal places. The :.2f means "format as a float with 2 decimal places."
  7. else: - If the user chooses anything other than "1", we convert lbs to kg.

  8. The else block - Works the same way but in reverse!

Output

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

Convert (1) kg->lbs or (2) lbs->kg? 1
Kg: 70
70.0 kg = 154.32 lbs
Enter fullscreen mode Exit fullscreen mode

Or if you convert the other way:

Convert (1) kg->lbs or (2) lbs->kg? 2
Lbs: 150
150.0 lbs = 68.04 kg
Enter fullscreen mode Exit fullscreen mode

New Concepts Introduced 🎯

One-Line Functions:

  • Compact way to write simple functions
  • Format: def name(param): return value
  • Great for simple calculations and conversions

Function Parameters:

  • Values you pass INTO a function
  • The function uses these values to perform operations
  • Example: kg_to_lbs(70) passes 70 as the parameter

String Formatting with Decimals:

  • :.2f formats numbers to 2 decimal places
  • :.1f would be 1 decimal place, :.3f would be 3, etc.
  • The f means "floating-point number"

Real-World Applications🔗

Weight converters are super practical:

  • Travel: Understanding luggage weight limits in different countries
  • Fitness: Tracking weight in your preferred unit
  • Cooking: Converting recipe measurements
  • Science: Working with international standards

Key Takeaways💡

  • Functions can be written in one line for simple operations
  • Parameters let you pass data into functions
  • The :.2f formatting rounds numbers to 2 decimal places
  • Menu-driven programs give users choices
  • One conversion factor (2.20462) lets you convert both ways
  • F-strings can call functions AND format their output in one go
  • Building practical tools makes programming more rewarding!

What's Next?

Tomorrow on Day 9, I'll be building a temperature converter! We'll convert between Celsius, Fahrenheit, and maybe even Kelvin. More unit conversions ahead!

Let's Connect! 💬

I'd love to hear from you!

  • Have you ever needed to convert weights between units?
  • What other converters would be useful to build?
  • Did the one-line function format surprise you?

Drop a comment below! If you're coding along, try adding more units (like grams or ounces) to your converter and share your results!

Don't forget to follow me for daily updates. Day 9 continues building practical tools!

Happy Coding!

Top comments (0)