Skip to content

Commit dea2cc0

Browse files
authored
add project
1 parent d8a3794 commit dea2cc0

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
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)