温度计算器输出的温度不正确?



尝试用python制作一个温度转换计算器。有什么问题吗?我输入了20C,它告诉我20c=52f,我知道这是不正确的。代码如下:

def convert(changeto,temp):
    if changeto == "c":
        converted = (5/9)*(temp-32)
        print '%d C =  %d F' % (temp,converted)
    elif changeto == "f":
        converted = (9/5)*(temp+32)
        print '%d F = %d C' % (temp, converted)
    else:
        print "Error, type C or F for Celsius or Fahrenheit conversions."
print "Temperature Converter"
temp = float(raw_input("Enter a temperature: "))
changeto = str(raw_input("Convert to (F)ahrenheit or (C)elsius? "))
convert(changeto,temp)
raw_input("Any key to exit")

根据你的 Python 版本,5/9的计算结果可能为零。将其更改为5./9(点将5转换为浮点文本)。

另一个部门也是如此。

最重要的是,你拥有的两个公式不是相互逆的,需要重新检查。

最新更新