Python Forum
My first temperature converter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first temperature converter
#1
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()

Reply
#2
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.15
The same type of error is repeated in lines 24 and 33.
Reply
#3
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).
Reply
#4
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
Reply
#5
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
Reply
#7
(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
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


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
Reply
#8
(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 kel
Your 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.0
read more here https://docs.python.org/3.8/library/stdtypes.html#truth
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Temperature converter Help (ASAP) Edison_Weng 1 1,307 Apr-16-2018, 01:55 PM
Last Post: stranac
  how to get the highest monthly average temperature? nikhilkumar 2 3,117 Jul-25-2017, 02:33 PM
Last Post: DeaD_EyE
  Using a range and for loop - temp converter rattlerskin 5 6,902 Jan-20-2017, 09:15 PM
Last Post: sparkz_alot
  Temp Converter with Kelvin vader33 7 6,413 Oct-02-2016, 05:16 AM
Last Post: vader33

Forum Jump:

User Panel Messages

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