Unit 2 Task 14 - Solution

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Solution - Task 14: Coin race to ten

← Back to task


One possible program:

import random

heads_count = 0
tails_count = 0

while heads_count < 10 and tails_count < 10:
    coin = random.randint(0, 1)
    if coin == 0:
        print("Tails")
        tails_count += 1
    else:
        print("Heads")
        heads_count += 1

if tails_count > heads_count:
    print("Tails Wins!")
else:
    print("Heads Wins!")
print("Total flips:", heads_count + tails_count)