A quality control program is designed to estimate the total mass of a shipment by randomly selecting 4 packages from a batch of 8 packages. The weights of the 8 packages (in kg) are stored in a list.
The program below is intended to select 4 packages at random, calculate their combined mass, and print the result. However, the program contains a logic error.
Identify the line number that contains the logic error, and write the corrected line of code.
Your corrected line must be written in Python.
1 import random
2 weights = [1.2, 3.5, 2.0, 4.1, 1.8, 2.4, 0.9, 3.0]
3 total_mass = 0.0
4 chosen_count = 0
5 while chosen_count < 4:
6 idx = random.randint(0, 7)
7 total_mass = total_mass + weights[chosen_count]
8 chosen_count = chosen_count + 1
9 print(total_mass)
298 exam-style questions on AQA GCSE Computer Science Programming concepts. Each one has a worked solution and a mark scheme showing where the marks go.