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 can't add i
to list
, any idea?
我不能把我列入名单,有什么想法吗?
2 个解决方案
#1
6
Two things are wrong with your code:
你的代码有两个问题:
- You are doing a
list == []
which returns a True or False since==
is a comparison operator. In this case it returnsFalse
. You need to use=
to initialize a variable. - 您正在执行一个list ==[],它返回一个True或False,因为==是一个比较运算符。在这种情况下,它返回False。您需要使用=来初始化一个变量。
-
list
is the name of a built-in type in python, use something else as your variable name. - 列表是python中内置类型的名称,使用其他变量作为变量名。
Fixing both of them :
修复它们:
alist = []
def MultiplesNumber(a):
for i in range(1, a+1):
if a % i == 0:
return i
alist.append(MultiplesNumber(100))
gives the correct output.
提供正确的输出。
#2
0
list
is the inbuilt keyword which shadows your list
variable. You need to assign a list to a variable not check its equality.
列表是一个内置的关键字,它是你的列表变量的阴影。您需要将一个列表分配给一个变量,而不是检查其是否相等。
lst = []
def MultiplesNumber(a):
return [x for x in range(1, a + 1) if a % 2 == 0]
lst.append(MultiplesNumber(100))
print(lst)
#1
6
Two things are wrong with your code:
你的代码有两个问题:
- You are doing a
list == []
which returns a True or False since==
is a comparison operator. In this case it returnsFalse
. You need to use=
to initialize a variable. - 您正在执行一个list ==[],它返回一个True或False,因为==是一个比较运算符。在这种情况下,它返回False。您需要使用=来初始化一个变量。
-
list
is the name of a built-in type in python, use something else as your variable name. - 列表是python中内置类型的名称,使用其他变量作为变量名。
Fixing both of them :
修复它们:
alist = []
def MultiplesNumber(a):
for i in range(1, a+1):
if a % i == 0:
return i
alist.append(MultiplesNumber(100))
gives the correct output.
提供正确的输出。
#2
0
list
is the inbuilt keyword which shadows your list
variable. You need to assign a list to a variable not check its equality.
列表是一个内置的关键字,它是你的列表变量的阴影。您需要将一个列表分配给一个变量,而不是检查其是否相等。
lst = []
def MultiplesNumber(a):
return [x for x in range(1, a + 1) if a % 2 == 0]
lst.append(MultiplesNumber(100))
print(lst)