Search code examples
pythonfunctiontemperature

Python Temperature converter output is none


Im trying to create a function which will convert temperatures based on the temperature that is being converted from to the temperature which we want. However the output of my code is none when I test it.

def convertTemperature(T, unitFrom, unitTo):
if unitFrom=="Fahrenheit" and unitTo=="Celsius":
    T=(T-32)/1.8
    return T
elif unitFrom=="Kelvin" and unitTo=="Celcius":
    T=T-273.15
    return T
elif unitFrom=="Celcius" and unitTo=="Fahrenheit":
    T=1.8*T+32
    return T
elif unitFrom=="Kelvin" and unitTo=="Fahrenheit":
    T=1.8*T-459.67
    return T
elif unitFrom=="Celcius" and unitTo=="Kelvin":
    T=T+273.15
    return T
elif unitFrom=="Fahrenheit" and unitTo=="Kelvin":
    T=(T*459.67)/1.8
    return T
    

the unitFrom parameter is the current temperature, T is the temperature and unitTo is the temperature to which I want to convert. I tried testing it with print(convertTemperature(50.0, "Fahrenheit", "Celcius")) but the output was none.


Solution

  • It is just a spelling mistake Celsius not Celcius

    This will work

    def convertTemperature(T, unitFrom, unitTo):
      if unitFrom=="Fahrenheit" and unitTo=="Celsius":
          T=(T-32)/1.8
          return T
      elif unitFrom=="Kelvin" and unitTo=="Celsius":
          T=T-273.15
          return T
      elif unitFrom=="Celsius" and unitTo=="Fahrenheit":
          T=1.8*T+32
          return T
      elif unitFrom=="Kelvin" and unitTo=="Fahrenheit":
          T=1.8*T-459.67
          return T
      elif unitFrom=="Celsius" and unitTo=="Kelvin":
          T=T+273.15
          return T
      elif unitFrom=="Fahrenheit" and unitTo=="Kelvin":
          T=(T*459.67)/1.8
          return T
    
    print(convertTemperature(50.0, "Fahrenheit", "Celsius"))