Warning - This site is moving to https://getthecodingbug.anvil.app
Topics covered
​ Writing a simple text-based game: Hangman
Hangman game flow, on paper
Typically, you would write a few dashes or hyphens, like this, and say you have 10 attempts to guess this phrase:
Can you guess this phrase? --- --- ----- -- ---- ---
Then ask them to guess a letter, and if they guess a correct letter, say 'e', you replace a hyphen with the correctly guessed letter:
Can you guess this phrase? --- --e ----e -e ---- ---
Add the letter to a list of letters used:
Letters used: e
Decrease the count of guesses left:
Guesses left: 9
and repeat the process until they have correctly guessed the phrase or have run out of guesses.
In case you were wondering, the phrase above, can be revealed by highlighting this text: " may the force be with you""
Hangman game in Python
Lets try to do code this in Python.
First, we need a variable to hold the number of guesses left, and set it to 10 (or whatever level of difficulty you want)
guessesLeft = 10
Then set the phrase to be guessed
secretPhrase = "happy new year"
It is best to stick to all lower-case letters, otherwise you will need to make you code more complicated to check for this.
Next we need an empty variable to hold the letter used
lettersUsed = ""
We need a function to convert the secretPhrase into hyphens to hide it from the user.
See the replaceWithHyphens function definition in the example below
Finally we need a loop that prompts the user to try different letters, show where the appear in the phrase and decrement the number of guesses left
Below is an example of how to set this up:
# Hangman by <your name> 2018
def replaceWithHyphens(phrase):
newPhrase = "----- --- ----"
# you need to think of some code to add here
return(newPhrase)
secretPhrase = "happy new year"
lettersUsed = ""
guessesLeft = 10
partialPhrase = replaceWithHyphens(secretPhrase)
print("Can you guess this phrase ?")
while(guessesLeft > 0):
print(partialPhrase)
print("Letters used: " + lettersUsed)
letter = input("You have " + str(guessesLeft) + " guesses left, enter a letter: ")
You will need another function to replace the hyphens in the masked phrase with the letters tried by the user.
I have named it 'tryGuessedLetter' in the example below.
We can break out of the loop at any time if the user has correctly guessed the phrase.
We also need a test at the end of the loop to see if the user has run out of guesses, and tell them the correct phrase.
# Hangman by <your name> 2018
def replaceWithHyphens(phrase):
newPhrase = "----- --- ----"
# you need to think of some code to add here
return(newPhrase)
def tryGuessedLetter(secretPhrase, letter, partialPhrase):
newPhrase = partialPhrase
# you need to think of some code to add here
return(newPhrase)
secretPhrase = "happy new year"
lettersUsed = ""
guessesLeft = 10
partialPhrase = replaceWithHyphens(secretPhrase)
print("Can you guess this phrase ?")
win = False
while(guessesLeft > 0):
print(partialPhrase)
print("Letters used: " + lettersUsed)
letter = input("You have " + str(guessesLeft) + " guesses left, enter a letter: ")
guessesLeft = guessesLeft - 1
lettersUsed = lettersUsed + letter
partialPhrase = tryGuessedLetter(secretPhrase, letter, partialPhrase)
if (partialPhrase == secretPhrase):
print("")
print("Congratulations - you guessed correctly!")
win = True
break
if (win != True):
print("")
print("You failed - the answer was: " + secretPhrase)
Below is the complete program with my versions of the two functions.
There is no right or wrong way to do this, if your code does the job then that is fine.
How could you enhance this program?
How about adding code to read a random phrase from a list you have already created?
You can use the import random statement, at the beginning of the program, and use n = random.randInt(10), for example to get a number from 0 to 9, which you can use to decide which phrase to use from your list.
You could also save you list in a text file and read that file to populate the list before your program starts the main loop.
# Hangman by <your name> 2018
def replaceWithHyphens(phrase):
newPhrase = ""
for n in range(len(phrase)):
if (phrase[n] == " "):
newPhrase = newPhrase + " "
else:
newPhrase = newPhrase + "-"
return(newPhrase)
def tryGuessedLetter(secretPhrase, letter, partialPhrase):
newPhrase = ""
for n in range(len(secretPhrase)):
if (letter == secretPhrase[n]):
newPhrase = newPhrase + letter
else:
newPhrase = newPhrase + partialPhrase[n]
return(newPhrase)
secretPhrase = "happy new year"
lettersUsed = ""
guessesLeft = 10
partialPhrase = replaceWithHyphens(secretPhrase)
print("Can you guess this phrase ?")
win = False
while(guessesLeft > 0):
print("")
print(partialPhrase)
print("Letters used: " + lettersUsed)
letter = input("You have " + str(guessesLeft) + " guesses left, enter a letter: ")
guessesLeft = guessesLeft - 1
lettersUsed = lettersUsed + letter
partialPhrase = tryGuessedLetter(secretPhrase, letter, partialPhrase)
if (partialPhrase == secretPhrase):
print("")
print("Congratulations - you guessed correctly!")
win = True
break
if (win != True):
print("")
print("You failed - the answer was: " + secretPhrase)