My 100-Day Programming Journey: Day 7 - The Hangman Project
Introduction
Greetings, fellow coding enthusiasts! Welcome to Day 7 of my 100-day programming journey. As a non-tech student venturing into the world of programming, every day brings exciting challenges and opportunities for learning. Today, I'll take you through my latest adventure: creating the classic Hangman game in Python. Together, we'll dissect each line of code and explore the valuable lessons I've gained.
Complete Codes of the HangMan Project:
import random
#TODO-1: - Update the word list to use the 'word_list' from hangman_words.py
#Delete this line: word_list = ["ardvark", "baboon", "camel"]
from hangman_words import word_list
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
end_of_game = False
lives = 6
#TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.
from hangman_art import logo
print(logo)
#Testing code
# print(f'Pssst, the solution is {chosen_word}.')
#Create blanks
display = []
for _ in range(word_length):
display += "_"
while not end_of_game:
guess = input("Guess a letter: ").lower()
#TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.
if guess in display:
print(f"You've already guessed {guess}")
#Check guessed letter
for position in range(word_length):
letter = chosen_word[position]
#print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}")
if letter == guess:
display[position] = letter
#Check if user is wrong.
if guess not in chosen_word:
#TODO-5: - If the letter is not in the chosen_word, print out the letter and let them know it's not in the word.
print(f"You guessed {guess}, that's not in the word. You lose a life.")
lives -= 1
if lives == 0:
end_of_game = True
print("You lose.")
#Join all the elements in the list and turn it into a String.
print(f"{' '.join(display)}")
#Check if user has got all letters.
if "_" not in display:
end_of_game = True
print("You win.")
#TODO-2: - Import the stages from hangman_art.py and make this error go away.
from hangman_art import stages
print(stages[lives])
The Quest for a Word
Our Hangman game starts with the crucial element—the secret word. Instead of hardcoding a list of words, I've decided to take a more dynamic approach.
# Update the word list to use the 'word_list' from hangman_words.py
from hangman_words import word_list
chosen_word = random.choice(word_list)
By importing a list of words from another file, hangman_words.py
, I ensure that the game remains fresh and enjoyable with a variety of words. This separation of concerns keeps our code clean and easy to manage.
Artistic Flourish
No Hangman game is complete without its iconic hangman figure. To give our game some personality, I imported a cool ASCII art version of the Hangman logo from hangman_art.py
and printed it at the start of the game.
from hangman_art import logo
print(logo)
It's the little touches like this that make programming projects truly shine.
Avoiding Repetition
To make the game more user-friendly, I implemented a check to prevent players from guessing the same letter twice.
if guess in display:
print(f"You've already guessed {guess}")
This simple yet effective snippet prevents frustration and adds a polished feel to the game.
Handling Wrong Guesses
In Hangman, lives are precious. Each incorrect guess brings you one step closer to losing the game. To manage this, I added a feature that informs players when they guess a letter not in the word.
if guess not in chosen_word:
print(f"You guessed {guess}, that's not in the word. You lose a life.")
lives -= 1
if lives == 0:
end_of_game = True
print("You lose.")
By tracking lives and providing feedback, the game keeps players engaged and challenges their word-solving skills.
Bringing It All Together
Finally, we assemble the word with correctly guessed letters and print it for the player to see.
print(f"{' '.join(display)}")
This visual feedback ensures that players can see their progress and stay motivated.
The Art of Survival
The last piece of the puzzle is importing and displaying the hangman figure as the player makes incorrect guesses. This is achieved by importing stages
from hangman_art.py
and displaying the appropriate stage based on the remaining lives.
from hangman_art import stages
print(stages[lives])
This small detail adds a layer of excitement and suspense to the game.
Winning and Losing
To wrap it up, I check if the player has guessed all the letters or if they've run out of lives, and I display the corresponding message.
if "_" not in display:
end_of_game = True
print("You win.")
Lessons Learned
As a beginner in the programming world, I've gained valuable insights during this Hangman project. Modularizing code, incorporating user-friendly features, and adding a dash of artistry can make a project engaging and enjoyable.
Conclusion
Join me tomorrow for Day 8 of my programming journey, where I'll tackle another exciting project. With every line of code, we're taking steps towards becoming better programmers. Happy coding!
Subscribe to my newsletter
Read articles from Aftab Ahmed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aftab Ahmed
Aftab Ahmed
Student pursuing BBA -Healthcare |Tech Enthusiast| Learning Python from scratch | #100Days of Code Challenge