Step 9: Define play_game function

This function runs the full game of 1 or more rounds and shows summary.

  1. This functions takes one argument filename
  2. Reads the file and turn the file contents into a list of words where each word is cleaned and converted to lowercase
  3. Initialises:
    • rounds
    • number of games
    • number of wins
    • a dictionary to track wins in across 1 to 6 guesses
  4. Repeatedly call the appropriate function that plays a round
  5. After each round:
    • check if player won
    • print success or failure message showing the correct word
    • update number of wins if player won
  6. Ask the player if they want to continue
    • ‘Y’ to continue
    • ‘N’ to stop
    • else print error message and ask again until valid
  7. If player stops playing:
    • print summary showing win percentage and win distribution
  8. Function should end after printing the summary
💡 Hint
  • The dictionary counts how many wins happened in 1 guess, 2 guesses, and so on (up to 6).
  • Use a while True: loop so the game keeps running until the player chooses to stop.
  • At the start of each round, increase the round number by 1.
  • The function that plays a round takes two arguments and returns a tuple.
    • You need to consider what arguments it takes when calling and how to use its return value in your code.
ℹ️
  • The value returned by the function includes:
    • the game result
    • the chosen word
    • You can use indexing to access them.
  game = function(read, rounds)
  word = game[1]
  • If the round was won update:
    • number of wins
    • the correct value inside dictionary of wins
  • Always increase number of games after each round
  • The win percentage can be calculated using: percentage = round((number_of_wins / number_of_games) * 100)
  • The win distribution displays the number of wins across the number of guesses 1-6:
    • if the number of wins is 0, print the number
    • otherwise print # repeated by number of wins for the number of guesses, followed by the number

'#' * (num_repeats)

✅ Code Runner

Define play_game function

Type or paste your code and run.