我在这里有一个功能
def celToFah(x):
ftemps = []
for i in x:
ftemps.append((9/5 * i) + 32)
return ftemps
我在列表理解中称之为。
ctemps = [17, 22, 18, 19]
ftemps = [celToFah(c) for c in ctemps]
得到以下错误
'int'对象不可迭代
为什么我收到错误?
celToFah
期待一个名单,你给它一个int
。
要么改变celToFah
只是在int
s上工作,就像这样:
def celToFah(x):
return 9/5 * x + 32
ctemps = [17, 22, 18, 19]
ftemps = [celToFah(c) for c in ctemps]
或者将ctemps
直接传递给celToFah
:
def celToFah(x):
ftemps = []
for i in x:
ftemps.append((9/5 * i) + 32)
return ftemps
ctemps = [17, 22, 18, 19]
ftemps = celToFah(ctemps)