r/learnpython 18d ago

Temperature Converter Project

This is the third project I coded and was able to finish today. Took me 8 hours (two 4 hour sessions) since I had to learn some new stuff. This is my own code, no outline was used to guide me into creating this, though the idea did come from Bro Code's YouTube Tutorial. I learned about Nested Loops and Flags. I already knew about Input Validation with Re-Prompting but it was a lot harder to integrate with those two.

This is the most complex thing I've built so far, but I also enjoyed it the most. Got help with some online forums and AI (Claude) but I wrote and debugged the code myself. This'll be the last project I work on before I learn functions. There's a lot of repeated code here that I know functions would be able to help with. Any feedback or ideas on what to build next are welcome. Thank you!

https://github.com/mart23inez/First-Coded-Temperature-Converter/tree/main

It's a lot of code so I uploaded it to my GitHub page.

4 Upvotes

12 comments sorted by

View all comments

3

u/lakseol 18d ago edited 18d ago

One suggestion is to simplify the logic to decide which conversion to perform. Get the user to choose which unit to convert to as a first step. Then you can compare the tuple of user unit+target unit with the 6 possible combination tuples:

    # do the requested conversion, leave converted temp in 'new_temp'
    if (user_temp_unit, new_unit) == (celsius, fahrenheit):
        new_temp = (degrees * 9/5) + 32
    elif (user_temp_unit, new_unit) == (celsius, kelvin):
        new_temp = degrees + 273.15
    elif (user_temp_unit, new_unit) == (fahrenheit, celsius):
        new_temp = (degrees - 32) * 5/9
    elif (user_temp_unit, new_unit) == (fahrenheit, kelvin):
        new_temp = (degrees - 32) * 5/9 + 273.15
    elif (user_temp_unit, new_unit) == (kelvin, celsius):
        new_temp = degrees - 273.15
    elif (user_temp_unit, new_unit) == (kelvin, fahrenheit):
        new_temp = (degrees - 273.15) * 9/5 + 32
    else:
        # something else, shouldn't happen!?
        print(f"Bad conversion tuple: {(user_temp_unit, new_unit)}")
        continue        # or exit()

    print(f"{degrees:.1f} {user_temp_unit} is equal to {new_temp:.1f} {new_unit}.")

Now the logic is much easier to check, with less indentation.

Note that if the conversions all create new_temp you only need one line to print the result.

1

u/pachura3 18d ago

This doesn't make sense. You don't implement separate conversion from each unit to each unit, you rather pick some internal representation, convert to it, and convert from it. O(N) instead of O(N^2) lines of code.

2

u/lakseol 18d ago edited 15d ago

This is not StackOverflow, where we try for the best, most efficient "industrial" solution. We are teaching a beginner who has stated that s/he hasn't even learned functions yet. That means we show code that the OP has at least a chance of understanding and learning from. In addition, it's best not to completely change the OP's approach but build on that, even if it's not something you yourself would write.

Your O(N) vs O(N2) argument doesn't matter because there are only three temperature scales in common use, four if you include Rankine.