[英]python trying to pass list in as variable in function TypeError: 'int' object is not iterable
I'm trying to pass a list in as a variable (var1) but I keep getting TypeError: 'int' object is not iterable. 我试图将列表作为变量(var1)传入,但我一直在获取TypeError:'int'对象不可迭代。 So I added a list comprehension thinking I need to make it iterate in order for it to sum.
因此,我添加了一个列表理解功能,认为我需要对其进行迭代以使其求和。 It didn't work, I'm not sure what the type error means exactly if it doesn't mean that I need to iterate over the list from var1.
它没有用,我不确定类型错误到底是什么意思,如果这并不意味着我需要遍历var1中的列表。 The splat works, it converts the tuple to a list then sums the list.
splat起作用,它将元组转换为列表,然后对列表求和。 It just won't work for a given list as var1.
对于给定的var1列表,它将不起作用。
class MathDojo(object):
def __init__(self):
self.count = 0
def add(self,var1,*args):
self.var1 = var1
self.var1 = [x for x in self.var1, sum(self.var1)]
self.args = list(args)
self.args = sum(self.args)
self.count += (self.var1 + self.args)
return self
def subtract(self,var1,*args):
self.var1 = var1
self.var1 = sum(self.var1)
self.args = list(args)
self.args = sum(self.args)
self.count += -(self.var1 + self.args)
return self
def result(self):
print self.count
return self
test = MathDojo()
test.add(2,2,2).subtract([3,2,1],3,1,2).add(1,3,5,6,7).subtract(3,4,6).result()
Traceback (most recent call last):
File "mathdojo.py", line 26, in <module>
test.add(2,2,2).subtract([3,2,1],3,1,2).add(1,3,5,6,7).subtract(3,4,6).result()
File "mathdojo.py", line 6, in add
self.var1 = [x for x in var1, sum(var1)]
TypeError: 'int' object is not iterable
test.add(2,2,2)
will make var1 to be equal to 2. 2 is not a list, so the line x for x in self.var1, sum(self.var1)
will have an error, because both for and sum requires list and 2 is an integer. test.add(2,2,2)
将使var1等于test.add(2,2,2)
不是一个列表,因此x for x in self.var1, sum(self.var1)
的行x for x in self.var1, sum(self.var1)
将有错误,因为两个并且sum需要list并且2是整数。 You can use test.add([2,2,2])
您可以使用
test.add([2,2,2])
This will serve your purpose: 这将满足您的目的:
self.var1 = [x for x in range(self.var1)]
self.var1.append(sum(self.var1))
But later part of your code has many other bugs. 但是您的代码的后面部分还有许多其他错误。 So I wonder the code will run without fixing them.
因此,我想知道代码将在不修复它们的情况下运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.