Temp Converter with Kelvin - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Temp Converter with Kelvin (/thread-230.html) |
Temp Converter with Kelvin - vader33 - Oct-01-2016 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 RE: Temp Converter with Kelvin - Exponential_Sinusoid - Oct-01-2016 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. RE: Temp Converter with Kelvin - ichabod801 - Oct-01-2016 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. RE: Temp Converter with Kelvin - ichabod801 - Oct-01-2016 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 = FalseThat 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: ') RE: Temp Converter with Kelvin - vader33 - Oct-01-2016 (Oct-01-2016, 02:41 PM)ichabod801 Wrote: In the future, please use code tags. 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 RE: Temp Converter with Kelvin - ichabod801 - Oct-01-2016 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. RE: Temp Converter with Kelvin - Vexis - Oct-02-2016 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) RE: Temp Converter with Kelvin - vader33 - Oct-02-2016 (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! |