I am trying to make a loop that will append an increasing number (one, then two, then three, etc.) of zeros to the end of a list with each iteration. Thus the list's length should be 1, then 3, then 6, then 10, etc. (Each of these list items in turn will then have 1 added to it by a nested loop as long as the loop's conditional holds true, so the list will not stay a list of only zeroes for the loop's duration).
Here is my attempt:
def myfunction(limit: int):
integerlist = []
newzeroes = 0
increment_index = 0
while sum(integerlist) < limit:
newzeroes += 1
integerlist.extend([0]*newzeroes)
while sum(integerlist) < limit:
integerlist[increment_index % len(integerlist)] += 1
increment_index += 1
return integerlist
I expected this to return lists with lengths such as 1, 3, 6, 10, or etc., but instead it always returns a list with length of 1 regardless of the input value. Since
integerlist.extend([0]*newzeroes)
wasn't adding zeroes to the list, I also tried
integerlist = integerlist + [[0]*newzeroes]
but this throws the error "unsupported operand type(s) for +: 'int' and 'list'".
I am a complete beginner to coding. I suspect this might be a basic syntax problem, but I have not been able to pinpoint what my mistake might be (relevant posts such as "Python: append item to list N times" seem to suggest using the same kind of methods and syntax I'm trying to use).