用于转换温度的python 2.7函数

用于转换温度的python 2.7函数,python,python-2.7,Python,Python 2.7,我正在尝试编写一个函数,将数字从摄氏度转换为华氏度,并希望能够将结果打印到小数点后两位 我的密码是:- def Cel2Fah(temp): temp = "28.0" return Cel2Fah 但我没有得到结果。非常感谢您的帮助。您的函数实际上从未进行过计算。我想你在找这样的东西: celsius = float(input('Enter degree Celsius: ')) # calculate fahrenheit fahrenheit = (celsius

我正在尝试编写一个函数,将数字从摄氏度转换为华氏度,并希望能够将结果打印到小数点后两位

我的密码是:-

def Cel2Fah(temp): 

    temp = "28.0"

   return Cel2Fah

但我没有得到结果。非常感谢您的帮助。

您的函数实际上从未进行过计算。我想你在找这样的东西:

celsius = float(input('Enter degree Celsius: '))

# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
def Cel2Fah(temp): 
    fah = temp * 9.0/5.0 + 32

    return fah
Cel2Fah(28)
通过如下调用函数,可以计算华氏28C:

def Cel2Fah(temp): 
    fah = temp * 9.0/5.0 + 32

    return fah
Cel2Fah(28)
如果要在华氏温度下打印带2位小数位的29C,以下是您需要运行的所有代码:

def Cel2Fah(temp): 
    fah = temp * 9.0/5.0 + 32

    return fah

print "{0:.2f}".format(Cel2Fah(29.0))

函数中的转换公式在哪里实现?公式是temp=“((28.0*9)/5)+32”我尝试了它,但它没有显示其空白结果。看在上帝的份上,在编写更多代码之前,请遵循基本教程。但问题是,它没有返回两位小数的字符串,我用float(Cel2Fah(29.0))尝试了这一点@RiteshSingh您可以使用format函数:“{0:.2f}”将其转换为小数点后两位的字符串。format(Cel2Fah(29.0))它仍然不起作用我试过了too@RiteshSingh结果是什么?它可以转换温度,但不显示小数点后两位数的结果