Using a Hangman Epython game

Hangman was a really nice game in our home. We will catch a pen and paper and exchange by choosing the words to guess. Today, it is used in class arrangements to help children practice their spell, understand the tactic decisions by first-language choices, and consider the words that occur from the hints.
In this article, we will travel in a Hangman game by using Spython. This is the first friend's job where we will read the bases of Python language, such as explains variables, duties, and conditional statements.
Understanding the project
First, we will understand the game and get into the depths how to work.
In the usual game of the Hangman game, player 1 selects a name, hiding from Player 2, and produces spaces related to the number of characters in the Word they select. Player 2 We must guess the word by speculating one character at a time. Play 2 has a defined number of life at the beginning, and with an incorrect guess of characters, they lose health (until the man hangs). With some reasonable guess, lives always likewise and before that. If player 2 loses all their lives without guessing the name, the game is over, and lose. If they handle the name of the name, they win the game. This is a game framework in its traditional sense.
In this project, the computer will be player 1, creating a name guess, while we, the user, we will be player 2. Let us use this above through the flow of better understanding.
Drawing flow and explaining each step helps to change our thinking process, so it is always a good form of drawing. Now let's start installing the problem!
Step 1: List of random words and module
The first step in this work is that the computer has chosen a random word that the user will have to guess. For this purpose, we will need a list of names when the computer takes one word to be guessed, and the Python work called at times the word in the list provided.
To generate a list, I kept common nouns that are widely used in the English language and find a list. I used these words and created the Python list and will also be used in this work.
As the name shows, a Python list Is the datetype that keeps itself a set of objects. The color list will be defined in Python as colors = ["red", "yellow", "green", "blue", "pink"]. You can view the syntax of a listing and more information from the official Python page from the list.
word_list = [
"time",
"year",
"people",
"way",
"day",
"man",
"thing",
...
]
You can enter the project files from my Gitity repository. hangman_words.py It is the Python file containing a list of words where the computer will download the name of the game.
Now, if our list is created, we need a computer to select a random word from the given list of words word_list. Python has a module, especially for this purpose called “random”. We will invite the module and use it to allow the computer to choose the term from time to time word_to_guess From the list words_list. You can print word_to_guess While installing codes of the project is to improve understanding, and give it to play and computer!
import random
word_to_guess = random.choice(word_list)
print(word_to_guess)
For more information about random.choice() Work, click here.
Step 2: Producing Spaces
The next step is to produce spaces equal to the number of characters in word_to_guess For the user to get an idea of the number of letters in the Word should guess. In this case, we will explain the flexion called blanks which will serve as a container of these unnamed name. Will contain the amount of “_” equal to the number of characters in word_to_guess.
Counting the number of letters in word_to_guess that is randomly selected by a computer from words_listWe will use Python work len()calculated length of rope. More information on this structural work can be accessed with this link.
blanks = ""
word_length = len(word_to_guess)
As we now know the number of characters within word_to_guessWe will use this number to add the same amount of “_” to change blanks. For this purpose, we will use it for LOOP, Python performance that allows us to use things in a row, to us, a cord stored in variations word_to_guess. Click here to learn more about for loop and its syntax.
for i in range(word_length):
blanks = blanks + " _"
print("Word to guess: " + blanks)
The above block of the code will fall above the number of characters in the word_to_guess thread and generated the empty by each letter. It will print many _ as the number of characters, one with the blank as a letter of the word to guess. So by the word “time”, the printed on the screen will be:
Name to guess: _ _ _ _
Step 3: Moving the user guessing the letter
Now the game begins! The computer has chosen the name, produced by the owners of that Word. Now comes the time of the user to guess the word by guessing one letter at a time.
Asking User in writing, we will use input work in python and keep it in turn guess:
guess = input("Guess a letter").lower()
Click here to learn more about the built-in activity input().
Step 4: Check if the letter is in the name
When the user has guessed a book, we will look at whether the user is guessing and exploring that it is not word_to_guess.
To do this, we will use a for loop. In addition, we will build and some of the flexion, your_wordwhich will update with the accrued characters and an empty list called,letters_guessed What will keep the letters named welled by the user (this will be useful as we will see later).
Therefore, if the computer has selected the word “cheiscake”, as the game goes on and the user guesses the “C” characters, “this your_word including letters_guessed It looks like:
your_Word = c_ee_ECA_E
Books_and guessed = [“c”, “e”, “a”]
letters_guessed = []
your_word = ""
for letter in word_to_guess:
if letter == guess:
your_word = your_word + letter
letters_guessed.append(guess)
elif letter in letters_guessed:
your_word = your_word + letter
else:
your_word = your_word + "_"
So basically what the LOOP above is the Tata with word_to_guess One book at a time, and checks:
- If a book is estimated by a user
guessis inword_to_guessThe code will update the variableyour_wordAdding a letter to guess its appropriate place. We need to understand that the Eppython, the cords are actually a letter of characters, and many lists are working on the ropes. In addition, we will also add this book to guess it in the listletters_guessed. - If the book is, the user is guessing,
guessis inletters_guessedmeaning that the user already suggested the book before, then we won't need to add this toletters_guessedbut you just have to add a book toyour_wordin its appropriate place. - Other, if a document is not read by user is not in
word_to_guessSo it won't be inletters_guessedWe will produce gaps in places.
If the code above seems to be a little surprise, feel free to run just the above with the loop while describing the variable and productive printing: word_to_guess, guess, guessed_lettersbesides your_word and printing the variable where they are changed.

Step 5: Creating LOOP while running until the game is over
Let's look back to the FlowChart that created us understand the project. To charge for this project, we need to remember the following points:
- The user has a specified number of lives at first
- For each letter to guess the wrong, lives reduced by 1
- If the user runs out of life, the user loses the game is over
- If the user living left, the computer will ask the user to guess another letter
- For each letter to right, lives remain unchanged, and empty is replaced by a book in the field
blanks- If flexibility
your_wordEverything is full, the user wins the game, and the game is over - If flexibility
It has empty spaces, then the computer will ask the user to guess the next letteryour_word
- If flexibility
As we have created for LOOP earlier promotes the attention of the attention, now is the time to put the idea of life, and reduce it when the user guesses that the book is wrong.
Let's explain the number of user lives in variations number_of_lives. The user has 6 opportunities to lift the wrong letter to the name.
number_of_lives = 6
Now, the points mentioned above, and we need a variable or situation that we tell us to ask the user to guess when the game is over when the game is over when the game is over when the game is over. Let's write that with the help of Boolean variations.
Just, Boolean is a Pythonype in Python that lasts true or false. We will use this boolean fluctuation to continue the game while false and the opposite. At first, while the game begins, this variable will be Falsewhich means the game is not over.
game_over = False
We will now introduce you a while The loop in the form that will work as long as the game is not over, and will include the above mentioned conditions in this while Loop. Look more with while Loop from official Python text here.
while not game over:
print("nYou have ", number_of_lives, " lives remaining!")
guess = input("Guess a letter: ").lower()
your_word = ""
for letter in word_to_guess:
if letter == guess:
your_word = your_word + letter
letters_guessed.append(guess)
elif letter in letters_guessed:
your_word = your_word + letter
else:
your_word = your_word + "_"
print("Word to guess: ", your_word)
In the Code section, I have added the installation statement and a print statement, which will issue the targets until now in their position in their position in the word_to_guess.
Step 6: Handling Circumstances
The last step is to deal with different situations. What happens when the user's note is already raised by a user, or a letter is not in the name? Also, what if all letters are guessed and there are no other spaces inside your_word? This will mean that the user guesses the word and thus conquered.
We will add this situation to the following lines:
if guess in letters_guessed:
print(f"nYou've already guessed {guess}")
if "_" not in your_word:
game_over = True
print("nYou have guessed the word! YOU WIN!")

In addition, if a document is not read by the user is not in word_to_guessUser guess is wrong, then their lives will be reduced in 1, and we will notify the user that their guessing book is not in the name. If it reduces the lives of the wrong speculation to end the whole user's life, then the boolean variable game_over will be set as True And the game will end, with a user who lost the game.
if guess not in word_to_guess:
number_of_lives -= 1
print(f"nYou guessed {guess}, that's not in the word. You lose a life.")
if number_of_lives == 0:
game_over = True
print(f"nIT WAS {word_to_guess}! YOU LOSE!")
Remember to add uppercase blocks to the code within while Loop running until the game is not over. The full code and essential comments may be found in this final final level of GitHub a better understanding.
Store
The above coding project is made by basic Python language. We found other built-in activities, such as print()besides input()We used random module to produce random words to guess and used for including while The bars in different Itersions are subject to need. We also passed the conditional if, elif including else statements. These are all the basic tools that start should not know before reaching complex codes. It is possible and explained and called tasks in the program, but this was meant to be the first introduction in the Python language. If you are Python opponent, feel free to share the various alternatives to this work!



