You are on page 1of 6

Introduction:

Hey fellow explorers of the Python realm! Get ready to


dive into the world of Hangman, where coding meets
wordplay. I cooked up this Python program to challenge
your wits and bring back the nostalgia of this classic
guessing game. Can you crack the code and save the
hangman from his impending fate? Let's jump in and
see if our coding journey can outsmart the mystery
words!

So, fire up your code editor and brace yourself for a


journey filled with letters, logic, and a bit of guesswork.
As we embark on this coding adventure, let's unravel the
hidden words and see how our Python skills fare against
the challenge. Are you up for the task?
import random
words = ["apple", "banana", "cherry", "date",
"elderberry", "fig", "grape"]
word_index = random.randint(0, 6)
word = words[word_index]
guessed_letters = []
attempts = 6
print("Welcome to Hangman!")
while attempts > 0:
display = ""
for letter in word:
if letter in guessed_letters:
display += letter
else:
display += "_"
print("\nWord:", display)
stages = [
"""
--------
||
|
|
|
|
---
""",
"""
--------
||
|O
|
|
|
---
""",
"""
--------
||
|O
||
|
|
---
""",
"""
--------
||
|O
| /|
|
|
---
""",
"""
--------
||
|O
| /|\\
|
|
---
""",
"""
--------
||
|O
| /|\\
|/
|
---
""",
"""
--------
||
|O
| /|\\
| / \\
|
---
"""
]
print(stages[5 - attempts])
guess = input("Guess a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Please enter a single letter.")
continue
if guess in guessed_letters:
print("You already guessed that letter.")
continue
guessed_letters.append(guess)
if guess in word:
print("Correct!")
if all(letter in guessed_letters for letter in word):
print("\nCongratulations! You guessed the word:", word)
break
else:
print("Incorrect guess.")
attempts -= 1
if attempts == 0:
print("\nSorry, you ran out of attempts. The word was:",
word)
print(stages[6])
break
Conclusion:
Hats off, my fellow code enthusiasts! Whether you
emerged triumphant, spared the hangman's fate, or got
tangled in the complexities of the Pythonic word maze,
remember it's all part of the learning game. This
Hangman project is our collective journey through
coding, mistakes, and victories.

Take a moment to reflect on the lines of code you


crafted and the words you deciphered. It's not just about
the game; it's about the thrill of writing code that does
something cool. Until our next coding escapade, may
your Python endeavors be bug-free, and may you keep
pushing the boundaries of your coding knowledge.
Happy coding, fellow student developers

You might also like