Work out what everything in a shop is worth.
What to build
A loop with a running total over a list of records.
Requirements
- Read each
ITEM QTY PRICEline. - Print
Total value: Vwith two decimals.
Input
The first line has n. Each of the next n lines has an item, a quantity and a price.
Output
One line.
Constraints
n is from 1 to 1000. Quantities are from 0 to 10000, and prices are from 0 to 100000.
Example
Input:
2
pen 10 5
book 2 250.50
Output:
Total value: 551.00
Why it works
The value of the stock is the sum of quantity times price, and one running total does the job.
Examples
Example 1 · Input
2 pen 10 5 book 2 250.50
Expected output
Total value: 551.00
Example 2 · Input
1 bag 0 900
Expected output
Total value: 0.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Convert the quantity to int and the price to float.
Hint 2
Add quantity times price to a running total.
Solution
Show one correct solution (try it yourself first)
n = int(input())
total = 0
for _ in range(n):
item, qty, price = input().split()
total += int(qty) * float(price)
print(f"Total value: {total:.2f}")