Back

Code Challenge: Heads or Tails

Difficulty  

divider

Objective

Let the user call a coin flip, keep score across rounds, and ask whether they want to play again.

This is the same shape as Rock, Paper, Scissors from Activity 1.15 — a game inside a loop with a running score. Try building it without looking back at that activity.

Skills to Practice

  • Looping until the user quits
  • Generating a random result
  • Tracking running totals
  • Comparing string input

Tasks

  • Create a new Python program named cc-heads-or-tails.
  • Ask the user to guess heads or tails.
  • Flip a coin at random and say whether they won or lost.
  • Keep a running count of wins and losses, and show it after each round.
  • Ask whether they want to play again, and keep going until they say no.
  • When they quit, display the final score.

Sample Output

Sample Output
Welcome to Heads or Tails!
Guess the outcome (heads/tails): heads [Enter]
It's heads! You win!
Wins: 1 | Losses: 0
Do you want to play again? (yes/no): yes [Enter]
Guess the outcome (heads/tails): tails [Enter]
It's heads! You lose.
Wins: 1 | Losses: 1
Do you want to play again? (yes/no): no [Enter]
Thanks for playing! Final score: Wins: 1 | Losses: 1

Commence Challenge