【问题标题】:Python Temperature converter output is none [closed]Python温度转换器输出没有[关闭]
【发布时间】:2020-11-27 12:24:10
【问题描述】:

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

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")) 对其进行测试,但没有输出。

【问题讨论】:

  • 你有“摄氏度”和“摄氏度”。

标签: python function temperature


【解决方案1】:

这只是一个拼写错误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"))

【讨论】:

    【解决方案2】:

    你好,你用错了摄氏这个词 应该是摄氏度 你的程序是正确的,除了拼写错误

    【讨论】:

      猜你喜欢
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多