πŸ“Œ Step 9: Define play_game function

This fucntions 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
  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 play 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

  • First open the file and read the words into a list.
    • You can use .split()
  • Do not forget to close the file after reading.
  • 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 in 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 each number of guesses, followed by count
      ⭐ '#' * (num_repeats)
βœ… Checker

Define play_game function

Type or paste your code and run.