2024-04-30 02:19:36 发布
网友
一,。 我想做一个像这种格式的函数
def fahr2cels(fahr) : # code that converts fhar to cels cels = (5/9)*(fahr-32) return cels
temperatures = [62, 67, 73, 83, 87, 96, 100, 100, 97, 88, 84, 70] for t in temperatures : fahr2cels(t)
为什么即使字母t被定义为列表,也只有一个值出现
这在Python 2和Python 3中运行良好。 确保使用浮动类型以防止不必要的圆角(5.0/9.0)
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
这在Python 2和Python 3中运行良好。
确保使用浮动类型以防止不必要的圆角(5.0/9.0)
结果是:
相关问题 更多 >
编程相关推荐