Guess the word (Python)
Guess the word tutorial
- Understand and use sequence in an algorithm
- Understand and use iteration in an algorithm (FOR and WHILE loops)
- Understand and use selection in an algorithm (IF, Else and Else if)
- Understand why it is so difficult to create a computer that can seemingly think for itself
- Understand and use data structures in an algorithm (for example, Lists, Tables or Arrays)
EDUCATOR: COMPUTATIONAL THINKING COMPETENCIES:
COMPUTER SCIENCE EDUCATORS:
CSTA K–12 CS Standards:
AREA OF LEARNING AND EXPERIENCE: Science and Technology:
Computation is the foundation for our digital world.
Progression step 3
- I can use conditional statements to add control and decision-making to algorithms.
- I can identify repeating patterns and use loops to make my algorithms more concise.
- I can explain and debug algorithms.
Progression step 4
- I can decompose given problems and select appropriate constructs to express solutions in a variety of environments.
- I can select and use data structures that efficiently manage data in algorithms.
- I can plan and implement test strategies to identify errors in programs.
Progression step 5
- I can identify, define and decompose problems, choose appropriate constructs and express solutions in a variety of environments.
- I can use file-handling techniques to manipulate data in algorithms.
- I can test, evaluate and improve a solution in software.
How the game works
1. The game will start by picking a random word or phrase from a list.
2. Next, each letter in the word or phrase is replaced by a dash '-' or 'underscore '_'.
_ _ _ _ _ _ / _ _ _ _ _ _
3. Finally, the player must guess the word by suggesting different letters.
- If the player guesses a correct letter, the letter is revealed in the phrase.
- For each letter the player guesses wrong, the player will lose a life.
Before creating their 'guess the word game', start by having the students explore some of the functions they will be using.
Start by sharing the following line of code to replace a character at specific index in a string.
Explain to the students that to replace a character with a given character at a specified index, you can use python string slicing as shown below:
string = "pythonhxamples"
position = 6
character = "e"
string = string[:position] + character + string[position+1:]
Explain that, in our game, we need to replace each letter in our word or phrase with a dash '-' or 'underscore '_'.
- Challenge the students to modify their code (above) so that it changes the 3rd letter to an underscore '_'.
Explain to the students that in order to change all the letters in our word or phrase, they are going to need a loop.
Ask the students copy and run the following code (You can try the code yourself using the code window above):
string = "TRUMP"
character = "_"
for i in range (len(string)):
string = string[:i] + character + string[i+1:]
Explain to the students that because there are no spaces in between each of the hidden letters, we can't tell how many letters there are in our word!
Ask the students to replace the last line in their code with the following.
Important! (Inform students that they must ensure that they put a space between the two speech marks " "):
Explain to the students that in order to make our game more challenging, each time the player plays the game it will pick a new word at random from a list.
Start by having the students practice this by creating a random name picker.
Tell the students that they are now going to create a random name picker and that this will form the basis of their 'guess the word' game.
Share the following code for students to copy (You can test this for yourself using the code window below):
names = ["Bob", "Dave", "Stuart"]
Ask the students to comment their code, using the hashtag (#), to explain what each line of the code is doing and why the random generator is returning a value between 0 and 2 rather than 1 and 3.
Explain to the students that lists start at 0 NOT 1.
e.g. if we were to run the following code:
The computer would return the name “Bob”
- Ask the students to replace the last line with the following new code:
Have the students run their new script and explain what the new code does.
Finally, explain to the students that, in order for our program to work, we need to add (concatenate) the word “Minion” at the beginning of our randomly selected name.
- Ask the students to replace the last line with the following:
print(“Minion” + “ “ + random.choice(names))
- Ask the students to add some more names to their list.
Explain to the students that they now have all the components they need to create their very own guess the word game.
Share the following code for students to copy (You can try the code yourself using the code window below):
#create a list of hangman words
wordList = ["cat","dog","mouse"]
#choose a word from the list at random
wordChosen = random.choice(wordList)
#create an empty list to store the used letters
used = 
#create a variable to store and display the player's guesses
display = wordChosen
for i in range (len(display)):
#replace each letter with a '_'
display = display[0:i] + "_" + display[i+1:]
#put a space between each dash
print (" ".join(display))
#counter stops the game once all letters have been guessed correctly
attempts = 0
#keep asking the player untill all letters are guessed
while display != wordChosen:
guess = input("Please enter a letter: ")
guess = guess.lower()
#Add the players guess to the list of used letters
print ("Attempts: ")
#Search through the letters in answer
for i in range(len(wordChosen)):
if wordChosen[i] == guess:
display = display[0:i] + guess + display[i+1:]
print("Used letters: ")
#Print the string with guessed letters (with spaces in between))
attempts = attempts + 1
print("Well done, you guessed right!)
Challenge the students to...
- Modify their code so that it tells the player how many guesses they made.
- Add some more words or phrases to their list.
- Modify their code so that it replaces spaces (" ") in a phrase with a slash "/".
- Add a 'life counter' and remove a life every time a player guesses an incorrect letter. The game ends if the player guesses correctly or they run out of lives (which ever comes first).
Unless otherwise specified, everything in this repository is covered by the following licence: