How to create a game of hangman in python
bellagerken
Posted on June 9, 2023
def get_word():
The very first thing to be done is create a list of possible words to be stored as a variable called word_list. Then call the random.choice method in order to get a new word each time the user plays.
word = random.choice(word_list)
return word.upper()
def play(word):
Next create a method that contains all the code for playing the game. To start, you can declare a few variables and printed a few statements like this:
word_completion = "_" * len(word)
guessed = False
guessed_letters = []
guessed_words = []
tries = 6
print("Let's play Hangman!")
print(display_hangman(tries))
print(word_completion)
print("\n")
"word_completion" represents underscore symbols times the length of the random word that was chosen. "guessed" represents whether or not yet the player has correctly entered all the right letters of the word. The letters and words that the player guesses will be appended to a list, and tries represents how many more chances they have until the hangman is complete. Additionally, there are some statements that direct the user to play, and print(display_hangman(tries)) refers to the next method to be created in which 7 different hangman stages will need to be created and stored as a list. The index of each hangman will be related to the number of tries the player still has to guess the correct answer. Additionally, the empty underscores will be shown to the player (word_completion) before the game begins.
The next step is to start a while loop as the user has not guessed the answer and their tries to win are greater than 0. To begin, initialize a variable called guess and set it equal to input("Please guess a letter or word: ").upper() where the player will enter their guess. Then, create an if/else statement block that first checks if the length of the guess is equal to 1 and if it is apart of the alphabet. Within that 'if' statement, create another that checks if 'guess' is in 'guessed_letters', and print: print("You already guessed the letter", guess). If guess is not in the word: print(guess, "is not in the word."). Here, the 'tries' variable will decrease by 1 (which will also cause the hangman to gain a body part since tries is related to its index), and the empty guess_letters list will append the guess. Else, if the guess is in the word, create a print statement that notifies the player, append the guess to 'guessed_letters', and create a variable for creating a list of 'word_completion'.
In relation to the first if statement created, finish it by then creating an elif statement that checks if the length of the guess is equal to the length of the word (and is a part of the alphabet). Within that elif statement create another if statement that checks if guess is in guessed words in which print: print("You already guessed the word", guess). If not in the word, print: print(guess, "is not the word."). Decrease the tries by one and append the guess to 'guessed_words' list. Else, guess = True and word_completion = word.
else:
print("Not a valid guess.")
print(display_hangman(tries))
print(word_completion)
print("\n")
Lastly, create a print statement if the player correctly guessed the word that says something like:
if guessed:
print("Congrats, you guessed the word! You win!")
else:
print("Sorry, you ran out of tries. The word was " + word + ". Maybe next time!")
Now your hangman code is complete!
For reference, see below an example of how you can create a visual hangman:
def display_hangman(tries):
stages = [
"""
--------
| |
| π€
| πππ
| π
| ππ
-
""",
# head, torso, both arms, and one leg
"""
--------
| |
| π€
| πππ
| π
| π
-
""",
# head, torso, and both arms
"""
--------
| |
| π€
| πππ
| π
|
-
""",
# head, torso, and one arm
"""
--------
| |
| π€
| ππ
| π
|
-
""",
# head and torso
"""
--------
| |
| π€
| π
| π
|
-
""",
# head
"""
--------
| |
| π€
|
|
|
-
""",
# initial empty state
"""
--------
| |
|
|
|
|
-
"""
]
return stages[tries]
Posted on June 9, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.