113k views
0 votes
Count the Heavy Packages Given a list of integers called package_weights, write code that counts the number of values in the list that are greater than 50. Store the result in a variable named heavy_count. Assume that the list has been initialized. 1 package_weights = [20,60,10,55,90,44,30,77] 2 heavy_count = 0 3 4 for i in package_weights: #counting the package_weights more than 50. if i > 50: heavy_count += 1 5 6 Check My Solution Load My Latest Solution Reset Test Results: X You're using a variable named heavy_count. The result is not correct when the list is [25, 47, 58, 12, 60,52]. The result is not correct when the list is [17, 38, 22, 9, 25, 6, 15, 30, 27]. The result is correct when the list is [67, 50, 71, 43, 40, 53, 82, 36). Х Х The result is not correct when the list is [55, 61, 90]. The details of this test are hidden. 50% of tests passed.

User BufBills
by
9.0k points

1 Answer

1 vote

Final answer:

The provided code for counting heavy packages greater than 50 in weight seems correct. Ensure that heavy_ count is initialized to 0 before the loop starts and not modified elsewhere in your code.

Step-by-step explanation:

To count the number of heavy packages in the package_ weights list that are greater than 50, your loop and condition appear to be set up correctly. However, the incorrect test results suggest there might have been an issue with how the heavy_ count variable was initialized or used. Please ensure that heavy_ count is set to 0 at the beginning of your code and not being modified elsewhere accidentally.

The code example provided looks correct, so it might be an issue with the platform or the way the tests are run. Make sure to run your code independently in a Python environment to double-check the count. If it's still incorrect, review the initial value of heavy_ count and ensure that the loop and condition are exactly as shown: package_ weights = [20,60,10,55,90,44,30,77], heavy_ count = 0, for i in package_ weights: if i > 50: heavy_ count += 1. This code should correctly increase heavy_ count every time an item in package_ weights is greater than 50.

User Benyamin
by
7.6k points