我无法在列表中添加int

and*_*All 1 python data-structures python-3.x python-3.4

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'
Run Code Online (Sandbox Code Playgroud)

我不能添加ilist,任何想法?

sha*_*aan 6

您的代码有两个问题:

  • 你正在做一个 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))
Run Code Online (Sandbox Code Playgroud)

给出正确的输出.