Day 6 :-
Project :-
Hangman Project
Hangman Game
Import required modules and data:
Import random module
Import a list of words, visuals for stages, and logo graphic
Initialize game settings:
Set initial number of lives to 6
Print the logo
Select a secret word for the game at random from the given word list
Display the chosen word (for debugging)
Create a string with underscores to represent each letter in the word (the current state of guessed word)
Display the placeholder word
Set game_over to False
Create a list to track correctly guessed letters
Create a list from the chosen word, where each character of the word becomes an item in the list
Start the main game loop while the game is not over:
Display the current number of lives left
Ask the player to guess a letter, or type 'hint' for a hint
Prepare a list of unrevealed letters for use in hints
If the guess is already in the list of correct guesses:
Inform the user the letter has already been guessed
Continue to the next loop iteration
If the guess is 'hint' and player has at least one life left:
Reduce a life
Randomly reveal one correct yet-to-be-guessed letter
Add that letter to correct guesses
For each letter in the chosen word:
If it matches the guessed letter, reveal it and update correct guesses
If the letter is already in correct guesses, reveal it
Else, display an underscore
Show the updated word with revealed letters and underscores
If guess was 'hint', skip to the next round
If guess is not in the chosen word:
Lose one life and display a message
If no lives are left, set game_over to True and reveal the word
If all letters have been guessed (no underscores left), set game_over to True and print a win message
Print the visual representation of the current stage based on remaining lives
CREATION 1 — Add input validation + guessed-letters list
Goal: Make the game robust and friendlier.
Requirements
Accept only one alphabetical character per guess.
If the user types more than one character or a non-letter, print "Please enter a single letter." and ask again (i.e., prompt for another guess).
Maintain and display the list of letters already guessed each turn (e.g., Guessed: a, e, t).
If the user repeats a guess, print "You've already guessed 'x'." and skip further processing for that loop iteration (do not change lives or display again).
Why: teaches validation, loop control (continue) and user-friendly UI — essential developer skills.
Hints
Use str.isalpha() to check letters.
Use len(guess) == 1 to check single character.
Add if guess in correct_letters: print(...); continue near the start of the loop.
What to submit
Full updated code.
One-line explanation of the changes.
Short run example showing:
An invalid input (e.g., ab or 1) handled,
Repeat-guess message,
Guessed list printed.
CREATION 2 — One-time hint feature (costs a life)
Goal: Add a hint command that reveals one unseen letter at the cost of 1 life.
Requirements
Allow user to type "hint" (case-insensitive) instead of a letter.
When the player requests a hint:
If there are unrevealed letters, reveal one random unrevealed letter in the displayed word immediately (as if the player guessed it correctly).
Deduct 1 life from lives.
Add the revealed letter to correct_letters so it persists.
If there are no unrevealed letters (should not happen since game would be won), print a suitable message.
Hints may be requested multiple times (if lives remain).
Why: Teaches list scanning, random selection from a filtered list, and in-game feature implementation.
Hints
Build a list of unrevealed letters: loop through chosen_word and collect letters not present in correct_letters.
Use random.choice(unrevealed_list) to pick the one to reveal.
After revealing, update display logic so guessed letter shows up the same way a normal guess would.
What to submit
Full updated code that supports hint.
One-line explanation.
Example run where user types hint and you show display before/after and lives decreased.
Debugging Question
DEBUG 1 — Repeated guesses still processed
Buggy snippet
guess = input("Guess a letter: ").lower()
if guess in correct_letters:
print(f"You've already guessed {guess}")
rest of code proceeds and may reduce lives or append again...
How to reproduce
Suppose correct_letters = ['e']. Type e as a guess. You see the message, but lives may still be reduced or the guess processed.
What’s wrong
You print the “already guessed” message but you don’t stop the loop from processing the guess again.
What to change
After printing the message, skip the rest of the loop for that iteration (use continue) so nothing else happens for a repeated guess.
Goal
Re-guessing a letter only shows the message and does not change lives or the display.
DEBUG 2 — Non-letter input reduces lives
Buggy snippet
guess = input("Guess a letter: ").lower()
later...
if guess not in chosen_word:
lives -= 1
print(f"You guessed {guess}, that's not in the word. You lose a life.")
How to reproduce
Type 12 or ab as input. Program treats it like a wrong guess and subtracts a life.
What’s wrong
Non-letter or multi-character input is being accepted, and the game punishes the player for it.
What to change
Validate input early: allow only a single alphabetical character (use guess.isalpha() and len(guess) == 1). If invalid, print "Please enter a single letter." and continue (do not decrement lives).
Goal
Only single letters are evaluated; invalid inputs are rejected without penalty.
Thank you, You have completed DAY 06
Top comments (0)