Python Forum
Temp Converter with Kelvin
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Temp Converter with Kelvin
#1
Hey guys, new to the forum, and to coding. My college programming class has us writing code that will convert from Fahrenheit to Celsius and to Kelvin. I thought I had everything correct, but when I run the code and give it a temperature to convert, all it does is read that same temp back as the converted one. Any help would be appreciated! Thanks!

[Image: nyik1w.png]

-Vader
Reply
#2
You're not changing the 'temp' variable at all, and then using it as the output. In essence, you're asking for the temperature and the measurement type, then asking for the conversion to make. You then make that conversion and print out the temperature that they gave you.
Reply
#3
In the future, please use code tags.

The problem is that you are outputting tempFormatted, which is calculated based on temp. You need to output the variables you calculated in the "#Make the conversion" section of your code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Also, look at your input checking section. Note that this is a much easier way to do it:

badInput = True
while badInput:
    which = input('Enter selection: ')
    if which in ['F', 'C', 'K']:
        badInput = False
That reduces three conditions to one condition. But then, if you just have one condition, you can stick that condition in the while statement:


which = 'X'
while which not in ['F', 'C', 'K']:
    which = input('Enter selection: ')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Oct-01-2016, 02:41 PM)ichabod801 Wrote: In the future, please use code tags.

The problem is that you are outputting tempFormatted, which is calculated based on temp. You need to output the variables you calculated in the "#Make the conversion" section of your code.

Sorry about the lack of code tags. I'll make sure to use those from now on. I see what you're saying about the output variables, but didn't I do that here?

Example:
if which =='K':
    fahrenTemp = (temp - 9/5) -459.67
    celsiusTemp = temp - 273.15
Reply
#6
You calculate the output variables, but you don't actually use them in the output. You output tempFormatted, but that is calculated off of temp, which is the original input. You need to calculate tempFormatted from fahrenTemp or celsiusTemp, and then print that. But not that you need to figure out which two calculated variables to output based on the which variable. You are basically already do that when you are calculating them, so you should probably just output them when you calculate them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I'm not exactly understanding your conversion methods, but this is how I would do it:
print("K for kelvin, C for Celsius, F for Fahrenheit ")

option = None
while not option in ['c', 'f', 'k']:
    option = input("Enter Selection: ").lower()


temp = float(input("Input Temperature in {}: ".format(option.upper())))
conversions = dict(kelvin=None, celsius=None, fahrenheit=None)

if option == 'k':
    conversions.update([('celsius', temp-273.16), ('fahrenheit', (temp+273.16)*(9/5)+32), ('kelvin', temp)])
elif option == 'c':
    conversions.update([('celsius', temp), ('fahrenheit', temp*(9/5)+32), ('kelvin', temp+273.16)])
elif option == 'f':
    conversions.update([('celsius', (temp-32)*(5/9)), ('fahrenheit', temp), ('kelvin', (temp-32)*(5/9)+273.16)])

print(conversions)
Reply
#8
(Oct-01-2016, 11:35 PM)ichabod801 Wrote: You calculate the output variables, but you don't actually use them in the output. You output tempFormatted, but that is calculated off of temp, which is the original input. You need to calculate tempFormatted from fahrenTemp or celsiusTemp, and then print that. But not that you need to figure out which two calculated variables to output based on the which variable. You are basically already do that when you are calculating them, so you should probably just output them when you calculate them.

You're exactly right. I see where I went wrong and made the changes. It took a little doing (this is only my 4th class in Intro to Programming lol), and here's what I came up with: 
if which =='F':
    celsiusTemp = (temp - 32) * 5/9
    kelvinTemp = (5/9 * (temp - 32) + 273.15)
    tempFormattedC = format(celsiusTemp, '4.1f')
    tempFormattedK = format(kelvinTemp, '4.1f')
    print('The converted temperature in Kelvin is', tempFormattedK)
    print('The converted temperature in Celsius is', tempFormattedC)
I just followed the same format for conversions with Celsius and Kelvin, and everything works as it should. Thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My first temperature converter TheLowEndTheory 7 289 Oct-18-2020, 04:39 PM
Last Post: buran
  Temperature converter Help (ASAP) Edison_Weng 1 1,340 Apr-16-2018, 01:55 PM
Last Post: stranac
  Using a range and for loop - temp converter rattlerskin 5 7,101 Jan-20-2017, 09:15 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020