Guess the word (Python tutorial)
How to create a 'guess the word' game in Python (Free python web tutorial).
Introduction:
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 '_'.
e.g.
DONALD TRUMPS
becomes
_ _ _ _ _ _ / _ _ _ _ _ _
3. Finally, the player must guess the word by suggesting different letters.
Rules:
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 '_'.
e.g.
DONALD TRUMPS
becomes
_ _ _ _ _ _ / _ _ _ _ _ _
3. Finally, the player must guess the word by suggesting different letters.
Rules:
- 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.
In this tutorial, you will learn how to create your very own 'guess the word' game using Python.
Part 1: Introducing the String Slicing
Before creating your guess the word game, let's explore some of the functions you will be using.
STEP 1
Let's start by writing a program to replace a character at specific index in a string.
To replace a character with a given character at a specified index, you can use python string slicing as shown below:
Before creating your guess the word game, let's explore some of the functions you will be using.
STEP 1
Let's start by writing a program to replace a character at specific index in a string.
To replace a character with a given character at a specified index, you can use python string slicing as shown below:
Type in the following code in the code window (below):
string = "pythonhxamples"
position = 6 character = "e" string = string[:position] + character + string[position+1:] print(string) |
Make sure you comment your code, using the hashtag (#), explaining what the code is doing.
In our game, we need to replace each letter in our word or phrase into a dash '-' or 'underscore '_'.
Challenge 1
Modify the code above so that it changes the 3rd letter to an underscore '_'.
STEP 2
In order to change all the letters in our word or phrase, we are going to need a loop.
Type in the following code in the code window (above):
In our game, we need to replace each letter in our word or phrase into a dash '-' or 'underscore '_'.
Challenge 1
Modify the code above so that it changes the 3rd letter to an underscore '_'.
STEP 2
In order to change all the letters in our word or phrase, we are going to need a loop.
Type in the following code in the code window (above):
string = "TRUMP"
character = "_" for i in range (len(string)): string = string[:i] + character + string[i+1:] print(string) |
Problem! Can you see the problem with our code?
Because there are no spaces in between each of the hidden letters, we can't tell how many letters there are in our word!
Replace the last line in your code (above) with the following (make sure you put a space between the two speech marks " "):
Because there are no spaces in between each of the hidden letters, we can't tell how many letters there are in our word!
Replace the last line in your code (above) with the following (make sure you put a space between the two speech marks " "):
print(" ".join(string))
|
Run your code again and comment your code, using the hashtag (#), explaining what the new code is doing.
Part 2: Creating a random name picker
To make our game more challenging, each time the player plays the game it will pick a new word at random from a list. Let's practice this by creating a random name picker.
STEP 1
Let's create a random name picker. This will form the basis of your 'guess the word' game.
Type in and run the following code:
To make our game more challenging, each time the player plays the game it will pick a new word at random from a list. Let's practice this by creating a random name picker.
STEP 1
Let's create a random name picker. This will form the basis of your 'guess the word' game.
Type in and run the following code:
import random
names = ["Bob", "Dave", "Stuart"] print(names[random.randint(0,2)]) |
Challenge:
Comment your code, using the hashtag (#), explaining what the code is doing and why the random generator is returning a value between 0 and 2 rather than 1 and 3.
Did you know?
Lists start at 0 NOT 1.
e.g. if we were to run the following code:
print(names[0])
The computer would return the name “Bob”
STEP 2
Replace the last line with the following new code:
Comment your code, using the hashtag (#), explaining what the code is doing and why the random generator is returning a value between 0 and 2 rather than 1 and 3.
Did you know?
Lists start at 0 NOT 1.
e.g. if we were to run the following code:
print(names[0])
The computer would return the name “Bob”
STEP 2
Replace the last line with the following new code:
print(random.choice(names))
|
Run the new script and explain what the new code does.
STEP 3
Finally, we need to add (concatenate) the word “Minion” at the beginning of our randomly selected name.
Replace the last line with the following:
print(“Minion” + “ “ + random.choice(names))
|
Run the new script and explain what the new code does.
Challenge:
Add some more names to the list.
Challenge:
Add some more names to the list.
Part 3: Creating the 'guess the word' game.
You now have all the components you need to create your very own guess the word game. Enter the following sample code into the code window below.
You now have all the components you need to create your very own guess the word game. Enter the following sample code into the code window below.
import random
#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 used.extend(guess) print ("Attempts: ") 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(used) #Print the string with guessed letters (with spaces in between)) print(" ".join(display)) attempts = attempts + 1 print("Well done, you guessed right!) |
Example Solutions

guess_the_word_solution.zip |
Challenge:
- Modify you code so that it tells the player how many guesses they made.
- Add some more words or phrases to your list.
- If your phrase contains a space, replace the space " " 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).
You may also be interested in:
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
Tags: programming language python, python programming language, python coding language, programming languages python, python code language, python programming language example, what is the python programming language, python scripting, python language tutorial, what type of programming language is Python, python class, python how to learn, learn python, learn python from scratch, where to learn Python, coding on computer, computer coding, best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in computers, how does computer code work, what is computer programmer.