1

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).

Chalcosoma
  • 15
  • 3
  • this `integerlist = integerlist + [[0]*newzeroes]` throws the other error because `integerlist` become a list of lists, then in the line `integerlist[increment_index % len(integerlist)] += 1` you try to add 1 to that list of lists. – Mauxricio Oct 17 '22 at 04:59
  • @Mauxricio Thank you for pointing that out. I changed it to ```integerlist = integerlist + ([0]*newzeros)``` to make it add the zeroes as list items rather than adding another list into ```integerlist```. This stops it from throwing the error, but I am still trying to figure out why it does not add zeroes to ```integerlist```. – Chalcosoma Oct 17 '22 at 05:23

1 Answers1

0

In your code the second while loop isn't needed, as it will always be True if the first while loop is True. So you can remove it, along with one of the variables. Also, I suspect increment_index % len(integerlist) should equal increment_value the way your code is structured, in that case it's also not needed.

def myfunction(limit: int):
    integerlist = []
    increment_index = 0
    while sum(integerlist) < limit: 
        increment_index += 1
        integerlist.extend([0]*increment_index)
        integerlist[increment_index-1] += 1
    return integerlist

print(myfunction(3))

Output:

[1, 1, 1, 0, 0, 0]

Following from the description of the output in the comment;

def myfunction(limit: int):
    integerlist = []
    increment = 1
    while True:
        integerlist.extend([0] * increment)
        for i in range(len(integerlist)):
            integerlist[i] += 1
            if sum(integerlist) >= limit:
                return integerlist
        increment += 1
bn_ln
  • 1,648
  • 1
  • 6
  • 13
  • I want ```myfunction(3)``` to output ```[2, 1, 0]``` (in the first iteration a zero would be appended to the empty list and then 1 added to that zero; in the second iteration two more zeros would be appended to the list, and 1 would be added the first and second list integers, at which point the conditional would be False, so nothing would be added to the third list integer. I should have clarified that the addition of 1 to each integer in the list needs to happen *one by one* for the full list in every iteration, and stop as soon as the conditional is False, even partway through the list. – Chalcosoma Oct 17 '22 at 06:09
  • OK, let me add to my answer – bn_ln Oct 17 '22 at 06:25