Skip to content

Commit aad143f

Browse files
authored
Merge pull request #1 from Infinitode/feat/add-hangman-game
feat: Add Hangman game project
2 parents 106f5dd + 6a083ae commit aad143f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

Beginner/13_hangman_game.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import random
2+
3+
def play_hangman():
4+
"""Plays a game of Hangman with the user."""
5+
6+
words = ["python", "javascript", "coding", "developer", "computer", "science", "algorithm", "programming", "github"]
7+
chosen_word = random.choice(words).lower()
8+
guessed_letters = []
9+
attempts = 6
10+
word_display = ["_" for _ in chosen_word]
11+
12+
print("Welcome to Hangman!")
13+
14+
while attempts > 0 and "".join(word_display) != chosen_word:
15+
print(f"\nWord: {' '.join(word_display)}")
16+
print(f"Guessed letters: {', '.join(sorted(guessed_letters))}")
17+
print(f"Attempts remaining: {attempts}")
18+
19+
guess = input("Guess a letter: ").lower()
20+
21+
# Validate input
22+
if not guess.isalpha() or len(guess) != 1:
23+
print("Invalid input. Please enter a single letter.")
24+
continue
25+
26+
if guess in guessed_letters:
27+
print(f"You've already guessed '{guess}'. Try another letter.")
28+
continue
29+
30+
guessed_letters.append(guess)
31+
32+
if guess in chosen_word:
33+
print(f"Good guess! '{guess}' is in the word.")
34+
for i, letter in enumerate(chosen_word):
35+
if letter == guess:
36+
word_display[i] = guess
37+
else:
38+
print(f"Sorry, '{guess}' is not in the word.")
39+
attempts -= 1
40+
41+
# Game over
42+
if "".join(word_display) == chosen_word:
43+
print(f"\nCongratulations! You guessed the word: {chosen_word}")
44+
else:
45+
print(f"\nGame Over! You ran out of attempts. The word was: {chosen_word}")
46+
47+
def main():
48+
"""Main function to run the Hangman game and allow replays."""
49+
while True:
50+
play_hangman()
51+
play_again = input("Play again? (yes/no): ").lower()
52+
if play_again != "yes":
53+
print("Thanks for playing Hangman!")
54+
break
55+
56+
if __name__ == "__main__":
57+
main()

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,46 @@ These projects are ideal for those new to Python. Each project includes a descri
430430
> [!NOTE]
431431
> Working code solutions are in the `/Beginner` folder.
432432
433+
### 13. Hangman Game
434+
- **Description**: Implement the classic Hangman game where the player guesses letters to reveal a hidden word.
435+
- **Solution**: https://github.com/Infinitode/Python-Projects/blob/main/Beginner/13_hangman_game.py
436+
- **Steps**:
437+
1. Create a list of words for the game.
438+
2. Randomly select a word.
439+
3. Display the word with blanks for hidden letters.
440+
4. Allow the user to guess letters.
441+
5. Keep track of guessed letters and remaining attempts.
442+
6. Reveal letters in the word as they are guessed correctly.
443+
7. End the game when the word is guessed or attempts run out.
444+
- **Tips:**
445+
446+
</summary>
447+
<details><summary>Tip 1:</summary>
448+
449+
Use `random.choice()` to select a random word from a list.
450+
451+
</details>
452+
<details><summary>Tip 2:</summary>
453+
454+
Use a `while` loop for the main game interactions.
455+
456+
</details>
457+
<details><summary>Tip 3:</summary>
458+
459+
Store guessed letters in a list to avoid repeat guesses and display them to the user.
460+
461+
</details>
462+
<details><summary>Tip 4:</summary>
463+
464+
Represent the hidden word as a list of characters or underscores, updating it as letters are guessed.
465+
466+
</details>
467+
<details><summary>Tip 5:</summary>
468+
469+
Keep a counter for incorrect guesses (attempts remaining).
470+
471+
</details>
472+
433473
## Intermediate Projects
434474
These projects are ideal for those with experience in Python. Each project includes a description, steps to follow, and tips for completing it.
435475

0 commit comments

Comments
 (0)