πŸ“Œ Step 6: Define get_player_guess function

  1. This function opens the file words.txt and reads all the words into a list, where each word is stripped of whitespace and converted to lowercase.
  2. The function then repeatedly prompts the user to enter a guess using a while True loop. The user input should also be converted to lowercase.
    πŸ“ Note
    • The words from the file and the user’s input are converted to lowercase to ensure consistency when comparing values during validation.
    • This prevents mismatches (e.g., β€œHEllo” vs β€œhello”), allowing valid words to be recognised regardless of how the user types them.
  3. The guess is validated using the following conditions:
    • The guess must be exactly 5 letters long
    • The guess must contain only alphabetic characters
    • The guess must exist in the list of valid words
  4. If the guess does not meet any of the conditions, print an appropriate error message and the user is prompted again.
  5. Once a valid guess is entered, the function returns the guess in lowercase.
πŸ’‘ Hint

  • You need to read words from a file called words.txt. Think about how to open a file safely in Python.
    Note: when you open a file you need to make sure to close it.
ℹ️
  • You can use a with open(...) statement so the file is automatically closed after reading.
  • Once the file is open, you can loop through each line in the file to access the words.
ℹ️
  • Words may include extra whitespace or newline characters. Use .strip() to clean each word
  • Convert each word to lowercase using .lower()
  • Store the cleaned words in a list using a list comprehension
ℹ️
  • You can chain methods/operations as long as each method/operation works on the object type returned by the previous one:
    • "hello world".split()[0].upper(): .split() works on str, "hello world", returns a list ["hello", "world"] β†’ [0] works on list, returns a str, "hello" β†’ .upper() works on str, returns str
  • Basic list comprehension format:
    new_list = [expression for item in iterable]
⭐
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

upper_fruits = []
for f in fruits:
    upper_fruits.append(f.upper())

is the same as:

upper_fruits = [f.upper() for f in fruits]
  • To check if the number of letters is not 5 β†’ len(...) != 5
  • To check if the guess is not alphabetic β†’ .isalpha()
  • To check if the guess is not a valid word β†’ not in
βœ… Checker

Define get_player_guess function

Type or paste your code and run.