Skip to content

Commit 582a420

Browse files
Merge pull request #1913 from srujana-16/Hangman
GSSOC'23 - Added python script to play hangman
2 parents e061380 + d2da632 commit 582a420

File tree

7 files changed

+142
-1
lines changed

7 files changed

+142
-1
lines changed

SCRIPTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,5 @@
104104
| 102\. | Scientific Calculator | This python program will help to solve the hardest problems of trigonometry, exponents functions, logarithmic functions, etc. in very easy way. This program has a best User interface of the scientific calculator and provide a best user experience | [Take Me](./Scientific_Calculator) | [Avdhesh Varshney](https://github.com/Avdhesh-Varshney)
105105
| 103\. | Chess Game | Chess Game is created using PYTHON script that provides user a interactive platform to play chess game. This program will create a interactive GUI of chess board for the user. Directly started in the terminal. | [Take Me](./Chess-Game/) | [Avdhesh Varshney](https:github.com/Avdhesh-Varshney)
106106
| 104\. | Audio splitting script | This Python script allows you to split large audio files into smaller segments based on silence, making them suitable for transcription or further analysis. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/Audio\Splitting) | [Srujana Vanka](https://github.com/srujana-16) |
107-
107+
| 105\. | Hangman game script | The Hangman game is a classic word-guessing game where the player tries to guess a secret word within a certain number of attempts. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/hangman) | [Srujana Vanka](https://github.com/srujana-16)
108108

hangman/Readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Terminal-games - Hangman
2+
## Description
3+
4+
This Hangman game is a simple implementation in Python where the player tries to guess a secret word by suggesting letters within a certain number of attempts. This is a classic word-guessing game that can be played in the terminal.
5+
6+
## To compile and run
7+
8+
- Clone the directory and `cd` into it.
9+
- To play Hangman:
10+
- `cd` into the hangman folder and run `python3 hangman.py`
11+
12+
## How to play?
13+
1. The game will start by displaying a series of underscores representing each letter of the secret word. You need to guess the letters and try to complete the word before running out of attempts.
14+
15+
2. Guess a letter by entering it through the terminal. If the letter is present in the word, it will be revealed in the appropriate position(s). If the letter is not in the word, you will lose one attempt.
16+
17+
3. Keep guessing letters until you either guess the word correctly or run out of attempts. The game will let you know if you win or lose.
18+
19+
4. After the game ends, you can choose to play again or exit the program.
20+
21+
## Built with - Python 3
22+
23+
Binary file not shown.
29.7 KB
Binary file not shown.

hangman/hangman.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import random
2+
from words import words
3+
from hangman_visual import lives_visual_dict
4+
import string
5+
6+
7+
def get_valid_word(words):
8+
word = random.choice(words) # Randomly choose a word for the given list
9+
while '-' in word or ' ' in word:
10+
word = random.choice(words)
11+
12+
return word.upper() # Return the word in uppercase
13+
14+
15+
def hangman():
16+
word = get_valid_word(words)
17+
word_letters = set(word) # Alphabets in the word
18+
alphabet = set(string.ascii_uppercase)
19+
used_letters = set() # Letters guessed by the user
20+
21+
lives = 7
22+
23+
# Get user input
24+
while len(word_letters) > 0 and lives > 0:
25+
print('You have', lives, 'lives left, and you have used these letters: ', ' '.join(used_letters)) # Join the used letters from the list
26+
27+
# Display what current word is for each guess
28+
word_list = [letter if letter in used_letters else '-' for letter in word]
29+
print(lives_visual_dict[lives])
30+
print('Current word: ', ' '.join(word_list))
31+
32+
user_letter = input('Guess a letter: ').upper() # Add the letter entered to the list
33+
if user_letter in alphabet - used_letters:
34+
used_letters.add(user_letter)
35+
if user_letter in word_letters:
36+
word_letters.remove(user_letter)
37+
print('')
38+
39+
else:
40+
lives = lives - 1 # Decrease number of lives by 1
41+
print('\nYour letter,', user_letter, 'is not in the word.')
42+
43+
elif user_letter in used_letters:
44+
print('\nYou have already used this letter. Guess another letter.')
45+
46+
else:
47+
print('\nInvalid letter. Enter a valid letter.')
48+
49+
# When number of lives is 0 or if the word is guessed
50+
if lives == 0:
51+
print(lives_visual_dict[lives])
52+
print('You died, sorry. The word was', word)
53+
else:
54+
print('YAY! You guessed the word correctly', word, '!!')
55+
56+
57+
if __name__ == '__main__':
58+
hangman()

hangman/hangman_visual.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Hangman representation after every turn on the terminal
2+
lives_visual_dict = {
3+
0: """
4+
___________
5+
| / |
6+
|/ ( )
7+
| |
8+
| / \\
9+
|
10+
""",
11+
1: """
12+
___________
13+
| / |
14+
|/ ( )
15+
| |
16+
| /
17+
|
18+
""",
19+
2: """
20+
___________
21+
| / |
22+
|/ ( )
23+
| |
24+
|
25+
|
26+
""",
27+
3: """
28+
___________
29+
| / |
30+
|/ ( )
31+
|
32+
|
33+
|
34+
""",
35+
4: """
36+
___________
37+
| / |
38+
|/
39+
|
40+
|
41+
|
42+
""",
43+
5: """
44+
___________
45+
| /
46+
|/
47+
|
48+
|
49+
|
50+
""",
51+
6: """
52+
|
53+
|
54+
|
55+
|
56+
|
57+
""",
58+
7: "",
59+
}

hangman/words.py

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)