Data science: Look up product names

Orders store only a product id. Join them to the product table to show names and totals.

What to build

A program that joins orders to products.

Requirements

  • For each order print ORDER: QTY x NAME = TOTAL (total = qty × price, two decimals), or ORDER: unknown product ID if the product id is not in the product table (matching is case-sensitive).
  • Finally print revenue: X, the sum of the totals, to two decimals.

Input

Line 1: p. Then p lines ID,NAME,PRICE. Next line: o. Then o lines ORDER_ID,PRODUCT_ID,QTY.

Output

One line per order, then the revenue.

Constraints

1 ≤ p ≤ 1000; 0 ≤ o ≤ 10000; product ids are distinct.

Example

Input:

2
P1,Pen,1.5
P2,Book,12
3
1,P1,4
2,P2,1
3,P9,2

Output:

1: 4 x Pen = 6.00
2: 1 x Book = 12.00
3: unknown product P9
revenue: 18.00

Why it works

Indexing the products by id turns each order's lookup into one dictionary access, and handling the missing case explicitly keeps unknown products from crashing the program or vanishing silently.

Examples

Example 1 · Input
2
P1,Pen,1.5
P2,Book,12
3
1,P1,4
2,P2,1
3,P9,2
Expected output
1: 4 x Pen = 6.00
2: 1 x Book = 12.00
3: unknown product P9
revenue: 18.00
Example 2 · Input
1
A,Apple,0.25
1
9,A,10
Expected output
9: 10 x Apple = 2.50
revenue: 2.50

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Build a dictionary from product id to (name, price) first.

Hint 2

An order whose product id is not in the dictionary is reported, and adds nothing to the revenue.

Solution

Show one correct solution (try it yourself first)
p = int(input())
products = {}
for _ in range(p):
    pid, name, price = input().split(",")
    products[pid] = (name, float(price))
o = int(input())
revenue = 0
for _ in range(o):
    order, pid, qty = input().split(",")
    if pid not in products:
        print(f"{order}: unknown product {pid}")
        continue
    name, price = products[pid]
    total = int(qty) * price
    revenue += total
    print(f"{order}: {qty} x {name} = {total:.2f}")
print(f"revenue: {revenue:.2f}")

Lesson: Joining tables →