Iterating through list multiple times ends unexpectedly

Viewed 24

I'm using a while loop to iterate through a list multiple times.

I am simulating the "box" method from combinatorics (math). In this method, we have a number of boxes (5) and a number of items. We want to go through each box, place one item, and then go to the next box, place an item, and so on. Doing this with 5 boxes and 7 items would result in the first two boxes having 2 items each, and the other three boxes to just have 1 item each.

My code works splendidly for up to 10 items, however, when I use 11 items or more, it just breaks completely. It just keeps outputting the result as if it had been 10 items.

I cannot for the life of me figure out what I am doing wrong, but then again I am really new to coding (3 weeks).

Code:

box_arr_ = [box1_, box2_, box3_, box4_, box5_] # boxes are objects of  "Box" class. 
no_of_boxes_ = len(box_arr_)
items = 11 
iterations = 0
import math

while iterations <= math.ceil(items/no_of_boxes_): # 11/5 = 2.2 ===> 3.
    iterations += 1
    for box in box_arr_:
        if items >0:
            items -= 1
            box.add_item(1) # Class method to add item to a box. 
        else:
            pass

for box in box_arr_:
    print(box.box_number, box.box_contents) # Outputs boxnumber (1 to 5) and no. of. items in box```



0 Answers
Related