NEBians
search Sign In Register

Python Word Game

Lesson 4 of 4 Coding game schedule18 min

Loading simulation…

sports_esportsPlay with the blocks and run your code

flagWhat you'll discover

  • arrow_forwardTest membership with the in operator on strings and lists
  • arrow_forwardSort guesses into found and wrong lists as the game runs
  • arrow_forwardBuild a masked word character by character with a loop
  • arrow_forwardDecide the round with an if/else on the final state

in: the one-word search engine

Python's in operator answers "is this inside that?" in a single word: "a" in "planet" is True, "z" in "planet" is False, and it works on lists too — guess in found. Behind the scenes Python walks through the string or list checking each element, but you get to write it as plain English. Membership tests are everywhere in real software: is this username taken? is this word in the dictionary? is this card already played? Your game engine runs on exactly this question.

Game state is just lists

A running game feels alive, but its memory is ordinary data. Your word game needs to remember two things: letters found and letters wrong — so it keeps two lists and appends to one or the other as each guess is judged. That pair of lists IS the game state. Snapshot them and you could save the game; print them and you can debug it. Every game you have played — chess apps, Wordle, massive online worlds — is state being updated by rules, exactly like your two append calls.

Building strings letter by letter

The masked word p_a__t comes from a classic pattern: start with an empty string masked = "", loop over each letter of the secret, and grow the string with masked += letter when the letter is found, or masked += "_" when it is hidden. Strings in the loop act like an accumulator — the same idea as totalling numbers, but concatenating text. The result reveals exactly what the player knows and hides the rest: one loop producing the game's entire display.

Think in phases

Notice the shape of your program: phase one judges all the guesses, phase two builds the masked word, phase three reports and declares the verdict. Decomposing a problem into ordered phases — each small enough to write and test alone — is the heart of algorithmic thinking, and it is the skill interviewers actually probe for. The full game loop (ask, judge, redraw, repeat until won or out of lives) is just these phases wrapped in a while loop; you have already built the hard part.

quizCheck your knowledge

1. What does "e" in "planet" evaluate to?
2. A guess is wrong. What should the engine do with it?
3. How is the masked word like p_a__t built?
4. Why split the program into judge, build and verdict phases?