You’ve probably played hangman. You know, the game where you have someone pick a word, and you have to guess the word by asking if there are certain letters in the word. Each time you get a letter wrong, or guess the word wrong, a limb of a dead person is added. When the whole body is added, you lose and you hate yourself because the word was so simple. Or if you have an expanded vocabulary, you might win.
Programming hangman was no easy challenge. I had to keep track of how many limbs have been hung, what letters the user has guessed, whether or not it’s a letter in the first place, and more. To start, I have a list of all possible letters:
After that, I had to create a visual representation of the hung limbs. I went with some cool ascii art (there are other ones with less limbs):
The next thing I had to do is get a list of words for the user to guess. I found an extremely long list (a very long string) and copied it. Then I converted all the words to lowercase and used the .split() method to get a list of words (there are four different strings: “EASYBANK”, “HARDBANK”, “COUNTRYBANK”, and “FOODBANK”):
After that, I needed to prompt the user to see what word bank they wanted to use:
The next objective was to select a random word from the list of words (word bank) that the user chose. This was quite easy:
Next, I had to introduce the game, and show the length of the word as an array. I choose the “countries” theme:
The next part of the game was to loop the game until the user guesses six incorrect letters in the word. This was extremely difficult:
Basically, the above code takes in the letter the user puts in, makes sure that the letter has not been guessed yet, and makes sure the letter is actually a letter. Then, if it is a letter, it gets sent to the gen_spaces_and_words function (shown above). That code checks if the letter is in the word. If it is, then it goes into a for loop and changes every underscore (there are the amount of underscores as there are letters in the word) that was generated in the beginning (assigned to the variable “temp”) to the letter that the user guessed. Take the following as an example:
If the word is “green”, the “temp” variable would output as [“_”, “_”,“_”, “_”, “_”] (if printed). If I guesses “e”, the “temp” variable would then be [“_”, “_”,“e”, “e”, “_”] (again if printed). Then the message “Correct! That letter is inside the word!” is displayed:
If the user was to guess a word that was not in the word, the message “That is incorrect would appear, and a new limb would show up (it increments the “stage” variable. There are 6 “stages” or guesses before you lose) (the ascii art of the hangman is a list with 6 drawings. I just index it with the “stage” variable):
If you run out of your tries (6), you get this:
If you win the game by guessing all the words, you get this:
Some skills that I learned in this project were the use of string indexes, and arrays. The use of them in this project is somewhat complicated (at least for me).
If you would like to run this game go to this link: