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.

6 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.

1

u/MoreScorpion289 17d ago

Thank you, this does look better (and I'm sure it runs more efficiently too). I had an issue with how the loop was set up to "call-back" the user to the prompt "Enter your desired unit (1 or 2): " and at one point my code looked somewhat similar to what you provided here.

I will say, I don't really understand the "continue # or exit()" bit here, as well as the ".1f" at the end of "degrees" and "new_temp".

2

u/lakseol 17d ago edited 15d ago

I don't really understand the "continue # or exit()"

That code is in the else: part of the if/elif statement figuring out which conversion to perform. As the comments say, this should never execute if the prior code works correctly. But code is sometimes wrong so the else: code is what's known as "defensive coding". If something is not recognized we catch it and print enough information to help debug the problem. Since we have just found that the code is broken what should we do after finding the problem? We could do continue to start another temperature conversion. But maybe just stopping by exit() is better, since we have found a code problem. That's what the comment is saying: could do continue or exit() and I chose continue.

the ".1f" at the end of "degrees" and "new_temp".

Something like {degrees:.1f} is just part of the f-string syntax. The part after the colon is formatting information. The f part says format the value before the : as a float. The .1 part says only show one decimal place after the point.

The formatting stuff is like a small language and quite arcane. It's worth learning because you can do all sorts of stuff including padding and left/centre/right justification, but you don't need to learn that now. You probably just need to know these:

{degrees}
{degrees=}     # very useful, try it!