banannamoofin 0 Newbie Poster

I am struggling with one part of my specification to complete my program :/, currently it does let me enter different items into containers and creates new containers as they are needed. However now I need the program to handle how it packs the items differently. Before when numbers such as 12,15,5,10,8,9 were entered output should have been 17,15,18,9, which worked fine. Now i need the output to be 20,20,19 from the same input. I'm not sure which is the easiet way to change this code to do that though:

containerList = [0] 

inputNumber = input("Please enter a weight between 1 and 20. Enter 0 to quit: ")
    

def findContainers(containerList,inputNumber):
     
    for i in range(0,len(containerList)):
        if inputNumber + containerList[i]  <= 20 :
            return i
       
        elif inputNumber + containerList[len(containerList)-1] > 20:
            return -1

        
def containerUpdate(containerList,inputNumber):
    x = 0
    while inputNumber != 0:
        if inputNumber not in range(1,21):
            inputNumber = input("Invalid number. Please try again. ")
            
        else:    
            i = findContainers(containerList,inputNumber)
            if i == -1:
                containerList = containerList + [inputNumber]
                i = len(containerList) - 1
            else:
                containerList[i] = containerList[i] + inputNumber

            x = x + 1
            print "Item ", x , ", Weight ", inputNumber ,": Container ", (i + 1)
            inputNumber = input("Please enter a weight between 1 and 20. Enter 0 to quit: ")

    print "Container weights: ", containerList

containerUpdate(containerList,inputNumber,)

Thanks in advance for any advice :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.