Two arrays of the same length can be combined item by item: a * b multiplies the first item of a by the first item of b, the second by the second, and so on, and gives back a new array. For an order, prices times quantities gives every line's total at once, with no loop.
Arrays also have methods that boil all their items down to one number: .sum() gives back the total, .mean() the average (the sum divided by the number of items), .min() the smallest item and .max() the biggest. np.round(x, 2) gives back x rounded to 2 decimals. That's handy for money: decimal numbers aren't stored exactly, so a sum can grow a long tail like 0.30000000000000004.
kg = np.array([1.5, 2.0, 0.5])
boxes = np.array([2, 1, 4])
weights = kg * boxes
print(weights) # [3. 2. 2.]
print(weights.sum()) # 7.0
print(weights.max()) # 3.0
Your program must print:
Prices: [12.99 6.5 29.9 2.25] Quantities: [2 3 1 4] Line totals: [25.98 19.5 29.9 9. ] Order total: 84.38 Average line: 21.1 Smallest line: 9.0 Biggest line: 29.9
Your task: below the two prints, multiply prices by quantities into line_totals and print it. Then print the order total and the average line, both rounded to 2 decimals with np.round, and the smallest and the biggest line.
Assignment
Follow the Coding Tutorial to work out an order's line totals and its total, average, smallest and biggest line, with no loop.
Hint.sum(), .mean(), .min() and .max() are methods, so they need their brackets. To round, give np.round the whole sum (or mean) as its first argument and 2 as its second.
Free plan: 30 tutor messages. PRO is unlimited.
