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