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
- Treat input that is only white space the same as if the user did not enter anything
- Make it lowercase
Solution
display_hangman function
get_secret function
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
Output
Lessons and Python Topics
- 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
Post a Comment