gpt4 book ai didi

python - 我无法将 int 添加到列表中

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:41 25 4
gpt4 key购买 nike

list == []

def MultiplesNumber(a):
for i in range(1, a+1):
if a % i == 0:
return i

list.append(MultiplesNumber(100))
TypeError: descriptor 'append' requires a 'list' object but received a 'int'

我无法将 i 添加到 list,知道吗?

最佳答案

您的代码有两处错误:

  • 您正在做一个 list == [],它返回 True 或 False,因为 == 是一个比较运算符。在这种情况下,它返回 False。您需要使用 = 来初始化一个变量。
  • list 是 python 中内置类型的名称,请使用其他名称作为变量名称。

修复它们:

alist = []

def MultiplesNumber(a):
for i in range(1, a+1):
if a % i == 0:
return i

alist.append(MultiplesNumber(100))

给出正确的输出。

关于python - 我无法将 int 添加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com