Several machines make parts, each with its own share of the output and its own defect rate. How likely is a random part to be defective?
What to build
A program that applies the law of total probability.
Requirements
- Print
P(defective) = Xwith X to 4 decimal places.
Input
Line 1: the number of machines n. Then n lines: a machine's share of the parts and its defect rate.
Output
One line.
Constraints
1 ≤ n ≤ 20; shares and rates are decimals from 0 to 1; the shares add up to 1.
Example
Input:
2
0.6 0.02
0.4 0.05
Output:
P(defective) = 0.0320
Why it works
A defective part came from exactly one machine, so the total probability adds each machine's share times its defect rate.
Examples
Example 1 · Input
2 0.6 0.02 0.4 0.05
Expected output
P(defective) = 0.0320
Example 2 · Input
3 0.5 0.01 0.3 0.02 0.2 0.03
Expected output
P(defective) = 0.0170
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Each machine contributes share × defect rate to the total.
Hint 2
The shares add up to 1.
Solution
Show one correct solution (try it yourself first)
n = int(input())
total = 0.0
for _ in range(n):
share, rate = map(float, input().split())
total += share * rate
print(f"P(defective) = {total:.4f}")