I have a function:
def save(self, text, *index):
file.write(text + '\nResults:\n')
if index == (): index = (range(len(self.drinkList)))
for x in index:
for y in self.drinkList[x].ing:
file.write('min: ' + str(y.min) + ' max: ' + str(y.max) + ' value: ' + str(y.perc) + '\n')
file.write('\n\n')
file.write('\nPopulation fitness: ' + str(self.calculatePopulationFitness()) + '\n\n----------------------------------------------\n\n')
Now, when I pass one argument as an index the function works as it is supposed to, but when I pass a tuple of 2 indices I get an TypeError: list indices must be integers, not tuple. What should I change?
The
save(self, text, *index)syntax means thatindexis itself atuple with all the arguments passed to
saveafter thetextone.So, for instance, if you have in your code:
then
indexwill be the tuple(1, 2, 3)and thefor x inwill correctly loop over valuesindex
1,2,3.On the other hand, if you haveL
then
indexwill be the 1-element tuple((1,2),)and thexin theloop will get the value
(1,2), hence theTypeError.