My first temperature converter - 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: My first temperature converter (/thread-30364.html) |
My first temperature converter - TheLowEndTheory - Oct-17-2020 NOTE: THIS IS A HOMEWORK, AND I AM NOT LOOKING FOR PEOPLE TO DO IT FOR ME! Hi guys! in my programming class I am required to create a temperature converter that will be able to convert from/to Celsius, Fahrenheit and Kelvin. However, each time I try to debug my code, I always need to input the temperature type and the number two times before I can finally see my results. Thanks for the help! (Excuse my english) Requirements : - First, I must first display an introduction to the program. - After, I need to ask the user to enter the temperature choice they wishes to enter. The choice must be validated and must accept lowercase and uppercase letters. (String.upper) - Finally, I need to make the user enter a temperature. Afterwards, the program must display the temperature that the user entered in the 3 different types of temperatures. The program must aslo ask the user if they want to start over. The 'Y' and 'N' will restart the program, while any other will let it end. Here is the work I did according to the requirements: def displayIntro(): print("This programm enables you to to convert from/to Celsius, Fahrenheit and Kelvin") def inputTempType(): print("C - Celsius") print("F - Fahrenheit") print("K - Kelvin") print("Enter the type of temperature to convert: ") tempType = input().upper() return tempType def inputTempValue(): print("PLS enter a number: ") tempValue = float(input()) return tempValue def celToFah(cel): return cel * 9/5 + 32 def celToKel(cel): return cel + 273.15 def fahToCel(fah): return fah - 32 * 5/9 def fahToKel(fah): return fah - 32 * 5/9 + 273.15 def kelToCel(kel): return kel - 273.15 def kelToFah(kel): return kel - 273.15 * 9/5 + 32 def displayResults(cel, fah, kel): print("Celsius =", cel) print("Fahrenheit =", fah) print("Kelvin =", kel) return cel and fah and kel def main(): displayIntro() tempType = inputTempType() tempValue = inputTempValue() continueChoice = "Y" while continueChoice == "Y" or continueChoice == "N": tempType = inputTempType() tempValue = inputTempValue() if tempType.upper() == "C": cel = tempValue fah = celToFah(cel) kel = celToKel(cel) elif tempType.upper() == "F": fah = tempValue cel = fahToCel(fah) kel = fahToKel(fah) else: #Kelvin kel = tempValue cel = kelToCel(kel) fah = kelToFah(kel) displayResults(cel, fah, kel) continueChoice = input("Would you like to continue?: Y(YES N(NO)").upper() if continueChoice == 'Y': main() elif continueChoice == 'N': break else: exit() main() RE: My first temperature converter - jefsummers - Oct-17-2020 I don't see a question, but your answers will be wrong. You are assuming calculations occur left to right. That is true of things at the same order of precedence, but not everything. In brief - addition and subtraction at same level Multiplication and division a level above Exponentiation a level above that Parentheses a level above that So, line 27 will calculate 32*5/9, subtract that from fah and then add 273. Wrong answer. Proper format would be return (fah - 32) * 5/9 + 273.15The same type of error is repeated in lines 24 and 33. RE: My first temperature converter - TheLowEndTheory - Oct-17-2020 Thanks for pointing out these flaws in my code! and my question is; Everytime it comes to debugging my code, I need to enter the temperature and my number two times before the reseults of the calculation are shown. So what do I need to change or apply in my code to fix these errors? (I only want a hint, and not the answer because I dont considerate this learning). RE: My first temperature converter - Yoriz - Oct-17-2020 You need to enter the temperature and number two times because the inputs are asked for twice def main(): displayIntro() tempType = inputTempType() # first time asking tempValue = inputTempValue() # first time asking continueChoice = "Y" while continueChoice == "Y" or continueChoice == "N": tempType = inputTempType() # second time asking tempValue = inputTempValue() # second time asking RE: My first temperature converter - buran - Oct-18-2020 I am not sure what you expect of this line return cel and fah and kel . Not that you need return in this function, in my opinion.
RE: My first temperature converter - TheLowEndTheory - Oct-18-2020 If I dont have the "return" and the cel, fah, kel, the program won't be able to return the calculation to convert the temperature type and the number that the user had input RE: My first temperature converter - TheLowEndTheory - Oct-18-2020 (Oct-17-2020, 10:41 PM)Yoriz Wrote: You need to enter the temperature and number two times because the inputs are asked for twice Hey thank you very much man!! your solution worked and my code have no error now. All I did is remove the tempType and the tempValue in in the displayIntro() haha RE: My first temperature converter - buran - Oct-18-2020 (Oct-18-2020, 03:26 PM)TheLowEndTheory Wrote: If I dont have the "return" and the cel, fah, kel, the program won't be able to return the calculation to convert the temperature type and the number that the user had input Note I refer only to displayResults function.def displayResults(cel, fah, kel): print("Celsius =", cel) print("Fahrenheit =", fah) print("Kelvin =", kel) return cel and fah and kelYour function just prints the arguments you pass. It does not need to return anything, nor you use what it currently returns. On line 65 you just call it displayResults(cel, fah, kel) . You don't assign what it returns to anything and simply throw it away.Now the other problem, look at the return statement: return cel and fah and kel cel , fah and kel are floats. So if all 3 variables can be evaluated as True, it will return kel . If any of cel or fah is 0 or 0.0 , i.e. that is evaluated as True, it will return it.>>> 1 and 2 and 3 3 >>> 1 and 0.0 and 2 0.0read more here https://docs.python.org/3.8/library/stdtypes.html#truth |