WORD GAME
Today’s Python coding challenge reproduces the popular word game, Hangman.
For anyone that does not know how to play this game . . .
- The computer randomly chooses a word from a list.
- Blanks are drawn to represent each letter in the word.
- Then, the user is asked to guess a letter in the word.
- If the word contains that letter, it displays on the screen in the correct location.
- If the word does not contain that letter, the user loses a life, and parts of the stick figure man start to appear hanging from a gallow.
- The game continues with the user guessing letters in the word.
- To win, the player must guess all the letters in the word before the stick figure drawing is complete, which is the loss of six lives.
This coding project allows the user to play the game against the computer. There are over 150 words in the database, and more can be easily added. One can enjoy the game at the bottom of this page.
Interesting Fact . . .
No name in the world is more fitting than the one bestowed upon the game of Hangman, which originated in Europe during the 17th century, and was “played” when a prisoner facing death demanded the “Rite of Words and Life.”
The prisoner was strung up over a 5–legged stand, then a blank word would be presented by the executioner. With each incorrect guess, a leg would be knocked away. If the word was guessed correctly, the prisoner would be set free, if not, once all 5 legs were chopped, he would hang. This “game” definitely had dark beginnings, but in time, has grown to a much more fun-loving game kids of all ages.
~ From History to History Class: the Origin of Classroom Games , Maximus Florence, December 9, 2019
*Some may be worried about the presentation of images of violence in this game, if so, you can draw a snowman instead of a hanging man.
REVIEW
This coding project builds on the programming concepts learned up to this point. This coding challenge reiterates flowcharts, variables, input statements, the print function, the len() function, lists, ranges, loops, if statements, negation, and the random module.
In Python and frankly in other programming languages, one must keep practicing and writing code to be proficient. Actually, for me, I put the #100 Days of Code challenge on hold for a week. Just in that short time, I had forgotten much of the Python syntax and therefore, I had to review previous lessons before moving forward.
IMPORT
In previous coding projects, the import statement proved useful. The coding contained the import random module to randomly choose a number or an item from a list.
In this challenge, the random import statement is used once again, but the programmer can also import other custom files. Custom files help create cleaner code by eliminating long lines of code embedded in the main coding structure. The files can be linked to the program code using the import statement.
For this project, one imported file contains the list of words that can be used to play the Hangman game.
In addition, other files were imported. They include the ASCI Art used to draw the beginning logo and the stick figures of the man. Also, the prewritten Repl.it file- clear() provides a clear screen between each letter guesses. This keeps the output window from being cluttered and lengthy while the user plays the game.
Repl.it is an interactive programming environment. The name REPL stands for Read-Eval-Print-Loop – an interactive, typically console-based, programming environment. This #100 Days of Code challenge utilized this workspace. You can find CLICK HERE to find out more or use Repl.it.
USER-FRIENDLY
The final output of any programming code needs to take into consideration all the possible outcomes. Equally important is making the output user-friendly.
For example in this coding challenge, when the user guesses a letter that is not in the unknown word, a print statement tells the user the letter they guessed, it is not in the word, and that they lose a life.
Another print statement shows when the user guesses a letter that they previously correctly guessed, and it says to try again.
And finally, a print statement executes when the game is complete. It states if the player won or lost and reveals the word chosen from the list by the computer.
One improvement to this game could include a print statement stating the guess was incorrect, and that the player has guessed this letter previously. Beware, this is currently not an option! If you guess the incorrect letter once again, you will lose another life.
Providing a list of guessed letters as the game progresses could also prove as an enhancement of this program, definitely adding a better user experience.
As mentioned before, importing the clear statement allows the screen to clear between letter guesses, providing for a seamless appearance in drawing the stick-figure man.
HANGMAN – DAY 7
HANGMAN – DAY 7
Have fun playing Hangman, the Day 7 – #100 Days of Code project.
It is satisfying writing code that actually functions as it should as well as providing some entertainment.
To play click the green button at the top of the window below. To restart the game click the green arrow once again. Good luck!
(Note- it may take a few seconds for Repl.it to update the files before the game begins. Be patient.)