A random variable takes a few values with given probabilities. Print its expected value and variance.
Input
The first line is a whole number n (1 to 20). Each of the next n lines has a value and its probability. The probabilities add up to 1.
Output
Two lines: E X and Var V, each to 3 decimal places.
Examples
Example 1 · Input
2 10 0.1 0 0.9
Expected output
E 1.000 Var 9.000
Example 2 · Input
6 1 0.1666667 2 0.1666667 3 0.1666667 4 0.1666667 5 0.1666666 6 0.1666666
Expected output
E 3.500 Var 2.917
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
E[X] = sum of value × probability.
Hint 2
Var(X) = sum of probability × (value − E[X]) ** 2.
Solution
Show one correct solution (try it yourself first)
n = int(input())
pairs = [tuple(map(float, input().split())) for _ in range(n)]
mean = sum(v * p for v, p in pairs)
var = sum(p * (v - mean) ** 2 for v, p in pairs)
print(f"E {mean:.3f}")
print(f"Var {var:.3f}")