列表理解中的调用函数

2024-10-02 10:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

这里我有一个函数

def celToFah(x):
    ftemps = []
    for i in x:
        ftemps.append((9/5 * i) + 32)
    return ftemps

我把它叫做列表理解。在

^{pr2}$

获取以下错误

“int”对象不可编辑

为什么我会出错?在


Tags: 对象函数in编辑列表forreturndef
1条回答
网友
1楼 · 发布于 2024-10-02 10:23:35

celToFah需要一个列表,您将给它一个int。在

请将celToFah更改为只处理int,如下所示:

def celToFah(x):
    return 9/5 * x + 32

ctemps = [17, 22, 18, 19]
ftemps = [celToFah(c) for c in ctemps]

或者将ctemps直接传递到celToFah

^{pr2}$

相关问题 更多 >

    热门问题