A prize hides behind one of three doors. You pick a door, the host opens another door with nothing behind it, and you may switch. Play recorded rounds and see which strategy wins more.
What to build
A program that plays the Monty Hall game for given rounds.
Requirements
- For each round, the car's door and your first pick are given. The host opens the lowest-numbered door that is neither your pick nor the car. Print
round K: host opens D, switch to E, where E is the door you would switch to. - Then print
stay wins S of N,switch wins W of Nandswitch estimate X(W ÷ N to 3 decimal places).
Input
Line 1: N. Then N lines: the car's door and your pick, each 1, 2 or 3.
Output
N + 3 lines.
Constraints
1 ≤ N ≤ 1000.
Example
Input:
3
1 1
2 1
3 2
Output:
round 1: host opens 2, switch to 3
round 2: host opens 3, switch to 2
round 3: host opens 1, switch to 3
stay wins 1 of 3
switch wins 2 of 3
switch estimate 0.667
Why it works
Staying wins only when the first pick was right, one time in three on average; switching wins whenever it was wrong, because the host has removed the other empty door.
Examples
3 1 1 2 1 3 2
round 1: host opens 2, switch to 3 round 2: host opens 3, switch to 2 round 3: host opens 1, switch to 3 stay wins 1 of 3 switch wins 2 of 3 switch estimate 0.667
2 3 3 1 3
round 1: host opens 1, switch to 2 round 2: host opens 2, switch to 1 stay wins 1 of 2 switch wins 1 of 2 switch estimate 0.500
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The host opens the lowest-numbered door that is neither your pick nor the car.
Hint 2
Switching moves to the one door that is neither your pick nor the opened door.
Solution
Show one correct solution (try it yourself first)
n = int(input())
stay = 0
switch = 0
for k in range(1, n + 1):
car, pick = map(int, input().split())
opened = min(d for d in (1, 2, 3) if d != pick and d != car)
other = next(d for d in (1, 2, 3) if d != pick and d != opened)
print(f"round {k}: host opens {opened}, switch to {other}")
if pick == car:
stay += 1
if other == car:
switch += 1
print(f"stay wins {stay} of {n}")
print(f"switch wins {switch} of {n}")
print(f"switch estimate {switch / n:.3f}")
Lesson: Simulation: estimating probabilities by experiment →