Step 6: Define get_player_guess function
File Handling π
File Open π
While Loops π
Modify Strings π
String Methods π
If Statements π
Membership Operators π
- This function opens the file
words.txtand reads all the words into a list, where each word is stripped of whitespace and converted to lowercase.
- The function then repeatedly prompts the user to enter a guess using a
while Trueloop. 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.
- 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
- If the guess does not meet any of the conditions, print an appropriate error message and the user is prompted again.
- 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:
Once the file is open (where
variableNameis 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\nat 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():.split()works on string β returns a list["hello", "world"][0]works on list β returns a string"hello"
.upper()works on string β returns string"HELLO"
words.txt, each word is on a new line, so both approaches will produce the same result. - You can chain methods/operations as long as each method/operation works on the object type returned by the previous one:
- Line by line:
You can store cleaned words using a list comprehension.
- 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: Consecutiveifstatements are evaluated independently, whileelifonly runs if all previousiforelifconditions were false. We use separateifstatements here because we want to check all conditions and report every issue with the input, rather than stopping after the first failure. - Use
continueto 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.