Work out the total cost of a shopping list.
What to build
A loop over two lists at once.
Requirements
- Read a line of quantities and a line of prices.
- Print the sum of each quantity times its price.
Input
Two lines of whole numbers, with the same number of values on each.
Output
One number.
Constraints
Each line has from 1 to 1000 values, each from 0 to 10000.
Example
Input:
2 1 3
10 50 5
Output:
85
Why it works
zip keeps the two lists in step, so each quantity is always multiplied by its own price.
Examples
Example 1 · Input
2 1 3 10 50 5
Expected output
85
Example 2 · Input
4 25
Expected output
100
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
zip the two lists together.
Hint 2
Add a * b for each pair.
Solution
Show one correct solution (try it yourself first)
quantities = [int(x) for x in input().split()]
prices = [int(x) for x in input().split()]
total = 0
for quantity, price in zip(quantities, prices):
total += quantity * price
print(total)