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
  • Think about how to open and read a file safely in Python.
  • Remember that files should be properly closed after use.
ℹ️
  • Use with open("filename.txt") as variableName: to open a file. This automatically closes the file after reading.

  • Alternatively:

    variableName = open("filename.txt")
    # file read code
    variableName.close()
  • Once the file is open (where variableName is the file object), you have two ways to read and clean the words:

    • Line by line: for line in variableName, which reads one line at a time as a string and allows you to process or append it to a list - use .strip() to remove the newline \n at the end of each line, and .lower() to lowercase.
    • Whole file: variableName.read().split(), which reads the entire file into a single string and splits it into a list of strings using whitespace by default (spaces, tabs, or newlines). For example, "1 hello".split() β†’ ["1", "hello"]. Since .split() already removes whitespace and newlines, only .lower() is needed. Apply .lower() to each word using a loop or 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():
          1. .split() works on string β†’ returns a list ["hello", "world"]
          2. [0] works on list β†’ returns a string "hello"
          3. .upper() works on string β†’ returns string "HELLO"
      Note: In words.txt, each word is on a new line, so both approaches will produce the same result.
  • You can store cleaned words using a list comprehension.

ℹ️
  • 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 β†’ word not in word_list
    Note: Consecutive if statements are evaluated independently, while elif only runs if all previous if or elif conditions were false. We use separate if statements here because we want to check all conditions and report every issue with the input, rather than stopping after the first failure.
  • Use continue to restart the loop and prompt the user again if a condition is not met.
βœ… Code Runner

Define get_player_guess function

Type or paste your code and run.