Hangman Game With Python

 

Problem and Solution

 Here are the instructions of what is expected of this game:

You have been assigned a task to implement the Hangman game. In hangman, one player chooses a secret word and the other player guesses letters. Each time the second player guesses a correct letter, all instances of that letter in the secret word are revealed. If the second player guesses a letter that is not in the word, then a portion of the hangman is drawn. The game ends once the entire hangman is drawn or the second player guesses all the letters that are in the word.

NOTE:

  • The second player has 7 guesses to guess the correct word
  • After each incorrect guess, the pieces of the hangman are drawn in the following order; Rope, Head, Torso, Left arm, Right arm, Left leg, Right leg
  • At the beginning of the game, the first player should be asked to enter the secret word
  • The letters in the secret word should be displayed as '?' until the user guesses them 
  • At the beginning of each round you should display the following in this order
    • The hanged man
    • The partially guessed secret word
    • The characters guessed so far in sorted order
  • If the user guesses a letter they have already guessed they should be told they already guessed that letter and be asked to enter a new guess 
Assumptions 
Input will not always be valid and if invalid input is entered you should keep asking the user for input until they enter valid input 

Input Validation 
Secret word: Any string that does not contain '?' or white space characters in the middle, a white space before and after the word should be stripped from the input before considering whether it is valid or not 
Guess: a single letter that has not already been guess: Strip white space from the guess before checking it.
  • Treat input that is only white space the same as if the user did not enter anything
  • Make it lowercase 

Solution

We begin by creating a functions that draw/display the current state a hang man. Here is a screenshot of a function. We have shown small section of the function. The whole code is available here Source Code

display_hangman function



get_secret function 


The function prompt the user to enter a secret word which the other player will try to guess. A secret word entered by the user must be validated to flush out garbage. For instance, the user is only allowed to enter a string of only alphabetical characters. String validation on get_secret function uses isalpha built in function. Here is a screenshot of the function. 




get_player_guess function 


The function accept one parameter of type list and then prompts the user to enter any letter which he/she thinks might be in a secret word. The function uses while loop to validate the user's input whereby a user is expected to only enter a single letter. Here is a screenshot of get_player_guess function. 


player function

The player function is a function that plays a hangman game by calling other functions to provide the necessary input such as secret word and guess letter. A well commented code that explains everything is provided on this link, Source Code

He is a screenshot of player script 


Output

This is what you get when you run the Hangman, see attached screenshot 




Lessons and Python Topics 

The Hangman game above provides an opportunity for beginners to learn several important concepts in Python programming. The following are some of the lessons and topics that can be achieved from the code above.
  • Functions: The code is organized into several functions like get_secret_word, display_hangman, get_player_guess, and player. This demonstrates how functions can be used to break down the code into smaller, more manageable pieces, promoting modularity and re-usability Functions. 
  • Input Validation: The get_secret_word and get_player_guess functions include input validation to ensure that the user's input meets certain criteria. This teaches the importance of validating user input to prevent errors and ensure the program behaves as expected Manipulating Strings. 
  • Loops: The code utilizes while loops for continuous execution until a certain condition is met (while True loops) and for iterating over elements in a sequence (e.g., for i, character in enumerate(secret_word)). This introduces the concept of loops for repetitive tasks Flow Control. 
  • Conditional Statements: Conditional statements (if, else, elif) are used throughout the code to make decisions based on certain conditions. This teaches how to control the flow of the program based on different scenarios Flow Control. 
  • Lists: Lists are used to store and manipulate sequences of characters, such as the stage list for storing the different stages of the hangman and the guessed_word list for representing the partially guessed word Lists. 
  • String Manipulation: Various string manipulation methods are used, such as strip(), lower(), replace(), and isalpha(), to process and validate user input, as well as to manipulate strings for display purposes Manipulating Strings. 
  • Error Handling: The code includes a basic error handling mechanism using try and except blocks to catch potential errors, such as EOFError when reading user input. 

Comments

Popular posts from this blog

Build a Shopping List Application with ArrayList in Java

File handling with C++ Programming