π Step 9: Define play_game function
This fucntions runs the full game of 1 or more rounds and shows summary.
- This functions takes one argument
filename - Reads the file and turn the file contents into a list of words
- Initialises:
- rounds
- number of games
- number of wins
- a dictionary to track wins in across 1 to 6 guesses
- Repeatedly call the appropriate function that play a round
- After each round:
- check if player won
- print success or failure message showing the correct word
- update number of wins if player won
- Ask the player if they want to continue
- βYβ to continue
- βNβ to stop
- else print error message and ask again until valid
- If player stops playing:
- print summary showing win percentage and win distribution
- Function should end after printing the summary
π‘ Hint
- First open the file and read the words into a list.
- You can use
.split()
- You can use
- 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.