Reputation: 3
I'm trying to pass a list in as a variable (var1) but I keep getting TypeError: 'int' object is not iterable. 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. The splat works, it converts the tuple to a list then sums the list. It just won't work for a given list as 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
Upvotes: 0
Views: 272
Reputation: 211
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.
Upvotes: 0
Reputation: 1405
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. You can use test.add([2,2,2])
Upvotes: 1