Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:如何使fahr2cels和cels2fahr函数_Python - Fatal编程技术网

python:如何使fahr2cels和cels2fahr函数

python:如何使fahr2cels和cels2fahr函数,python,Python,一,。 我想做一个像这种格式的函数 def fahr2cels(fahr) : # code that converts fhar to cels cels = (5/9)*(fahr-32) return cels 这就是我想做的 为什么即使字母t被定义为一个列表,也只有一个值出现?这在Python 2和Python 3中效果很好。 确保使用浮动类型以防止不必要的圆角(5.0/9.0) 结果是: 16.67 19.44 22.78 28.33 30.56 35.56

一,。 我想做一个像这种格式的函数

def fahr2cels(fahr) : 
    # code that converts fhar to cels
    cels = (5/9)*(fahr-32)
    return cels
  • 这就是我想做的

  • 为什么即使字母t被定义为一个列表,也只有一个值出现?

    这在Python 2和Python 3中效果很好。
    确保使用浮动类型以防止不必要的圆角(5.0/9.0)

    结果是:

    16.67
    19.44
    22.78
    28.33
    30.56
    35.56
    37.78
    37.78
    36.11
    31.11
    28.89
    21.11
    

    您的列表是
    fahr
    ,而不是
    temperature
    。您提供的代码根本不起作用。重新检查您发布的内容。您发布的代码在Python 2中不起作用,其中
    5/9
    0
    。您可能希望将其标记为Python3。
    def fahr2cels(fahr):
        return (fahr - 32) * 5.0/9.0
    
    def cels2fahr(cels):
        return (cels * 9.0/5.0) + 32
    
    temperatures = [62, 67, 73, 83, 87, 96, 100, 100, 97, 88, 84, 70] 
    for t in temperatures: 
        print(round(fahr2cels(t), 2))
    
    16.67
    19.44
    22.78
    28.33
    30.56
    35.56
    37.78
    37.78
    36.11
    31.11
    28.89
    21.11