Python温度转换器输出为无

2024-06-16 11:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个函数,该函数将根据正在转换的温度将温度转换为我们想要的温度。然而,当我测试代码时,它的输出是无的

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
    

unitFrom参数是当前温度,T是温度,unitTo是我要转换的温度。 我试着用print(convertTemperature(50.0, "Fahrenheit", "Celcius"))测试它,但结果是没有


Tags: and函数returnifdef温度测试代码fahrenheit
2条回答

这只是一个拼写错误Celsius而不是Celcius

这会奏效的

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"))

嗨,你用错词了 那就是塞尔修斯 除了拼写错误,你的程序是正确的

相关问题 更多 >