希望它能够双向转换,如果是摄氏,那么它转换成华氏温度,反之亦然。当涉及到Python时,我对它一无所知,这是我的一个类的一部分。使用高阶函数并学习如何实现它们。
def tConvert(x,y = "C"):
loop = len(x)
while loop > 0:
if y == "C":
result = float(round((9 * x) / 5 + 32))
else:
y == "C"
result = float(round((x - 32) * 5 / 9))
return result File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\Python 12.py", line 8, in tConvert
result = float(round((x - 32) * 5 / 9))
TypeError: unsupported operand type(s) for -: 'list' and 'int'发布于 2021-12-20 02:28:35
如果x是一个列表,那么只需一次运行一个转换列表。如果你要把结果保持为浮动,就没有必要绕一圈。
def tConvert(x,y = "C"):
result = []
for temp in x:
if y == "C":
result.append( 9 * temp / 5 + 32)
else:
result.append( (temp - 32) * 5 / 9)
return resulthttps://stackoverflow.com/questions/70335668
复制相似问题