generate functionFri, 17 Feb 2023

We have a backpack with a capacity of n liters, and our task is to pack it to the limit as much as possible. We receive things, the volume of which is also measured in liters, and we must put them in our backpack without the possibility of skipping. As soon as the total volume of the new thing added exceeds the capacity of the backpack, your program should print the word "Enough!" and then on separate lines the total amount of things that we were able to pack in a backpack, and their number Input data The number n is the capacity of the backpack. Next come an arbitrary number of lines - the volume of the next item. Output The line "Enough!" and then two numbers - the total volume of packaged goods and their quantity. Each value is displayed on a separate line.

def backpack(n): volume = [] count = 0 while True: volume_in = int(input()) if volume_in > n: print("Enough!") print(count, volume_in) break else: volume.append(volume_in) count += 1

Questions about programming?Chat with your personal AI assistant