0% found this document useful (0 votes)
26 views

$ Pip Install Random-Word

This document discusses coding a Hangman game in Python. It divides the coding logic into three parts: 1) a function to get a random word, 2) a function to determine winning/losing logic, and 3) calling the functions to iterate the game. For the random word function, it uses the random-word package and adds validations to remove special characters. The winning/losing logic function checks letter guesses against the word and allows guessing the full word. It provides the full Python source code at a given link.

Uploaded by

Gayatri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

$ Pip Install Random-Word

This document discusses coding a Hangman game in Python. It divides the coding logic into three parts: 1) a function to get a random word, 2) a function to determine winning/losing logic, and 3) calling the functions to iterate the game. For the random word function, it uses the random-word package and adds validations to remove special characters. The winning/losing logic function checks letter guesses against the word and allows guessing the full word. It provides the full Python source code at a given link.

Uploaded by

Gayatri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Do you remember the famous Hangman game you used to play

in childhood. I know many of you would take a trip down


memory lane after reading the ‘childhood’ . I am sure after
creating this game on your own you will be pleased enough like
you were in childhood after guessing the word! The most
important thing is it doesn’t just has basic vocabulary words but
has advanced words as well. So be prepared for it. It’s coding
time…..

I have divided this game in three parts for better coding logic. It
is always better to break the code in chunks so that it will be
easy for us to handle the logic.

 Function to get the random word

 Function which decides the winning/loosing logic

 Actual calling of the functions and iteration of the


game

Now the first part, to get the random word:

We have used the package ‘random_word’. You have to install it


using
$ pip install random-word
It is a simple python package to generate random English
words. For more information on this you can refer below:
Random-Word
This is a simple python package to generate random english words. If you
need help after reading the below, please find…
pypi.org

After getting the random word I have added some validations to


it because the word we are getting using ‘random_word’
package may contain some special characters like ‘-’ or ‘_’. In
Hangman we have limitation that we cannot use special
character in word.

Second part : which manages the winning/loosing logic

Second function contains the main logic where the game decides
whether player guessed the correct word or not.
While guessing the word we can guess either letter in word or
complete word after guessing bunch of letters. So I have created
two separate conditions through which we can guess complete
word or a single letter in word. These conditions depends on the
length of the input user is giving. You will understand this
function once you go through complete source code, I have
given the link for complete source code in the end of this article.
Only important part in this function is below:

I will explain this block of code:

chances: variable which defines the number of attempts we are


allowing player to guess wrong word or letter(here we have not
drawn actual hangman instead we are using specific number of
attempts)

guessed_letters : list of guessed letters

word : random word we got

In elif condition, we first find out the list of indices which


contains guessed letter from word. We use these indices for
replacing the blank space in non-guessed word with guessed
letters. We have used enumerate function here for that purpose.
For more information on this please refer :
Built-in Functions - Python 3.8.3 documentation
The Python interpreter has a number of functions and types built into it that
are always available. They are listed…
docs.python.org

The second condition of guessing complete word you have to


understand on your own. Consider it as a homework to sharpen
your logic. Do let me know in comments if you have any doubts
in logic.

Last but not the least : Actual calling of the functions and
iteration of the game
In this part we call our earlier function start_play() to start the
game. We ask player if he/she really wants to play the game and
then game will continue until the player choose ‘N’.

Now we are all set to run the game. You can run the script by
typing following command in bash window that is “python
<scriptname.py>”

So people this is simple yet effective implementation of Python.


The link for source code is :
https://gist.github.com/gayu19/f29a5a5ea7a34b8c80f1a726b5790c7a#file-hangman-py

You might also like