Skip to content

Commit 283be4c

Browse files
updated
1 parent ce97ead commit 283be4c

1,382 files changed

Lines changed: 136 additions & 8257 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
762 Bytes
Binary file not shown.

45-hangman-game/main.py

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,127 @@
1-
# Hangman Game
1+
# hangman in python
2+
3+
from words_list import words
4+
5+
import random
6+
7+
8+
hangman_art = {
9+
0: """
10+
-----
11+
| |
12+
|
13+
|
14+
|
15+
|
16+
=========
17+
""",
18+
1: """
19+
-----
20+
| |
21+
O |
22+
|
23+
|
24+
|
25+
=========
26+
""",
27+
2: """
28+
-----
29+
| |
30+
O |
31+
| |
32+
|
33+
|
34+
=========
35+
""",
36+
3: """
37+
-----
38+
| |
39+
O |
40+
/| |
41+
|
42+
|
43+
=========
44+
""",
45+
4: """
46+
-----
47+
| |
48+
O |
49+
/|\\ |
50+
|
51+
|
52+
=========
53+
""",
54+
5: """
55+
-----
56+
| |
57+
O |
58+
/|\\ |
59+
/ |
60+
|
61+
=========
62+
""",
63+
6: """
64+
-----
65+
| |
66+
O |
67+
/|\\ |
68+
/ \\ |
69+
|
70+
=========
71+
""",
72+
}
73+
74+
75+
def display_man(wrong_guesses):
76+
print(hangman_art[wrong_guesses])
77+
78+
79+
def display_hint(hint):
80+
print(" ".join(hint))
81+
82+
83+
def display_answer(answer):
84+
print("The answer was: " + answer)
85+
86+
87+
def main():
88+
answer = random.choice(words)
89+
hint = ["_"] * len(answer)
90+
wrong_guesses = 0
91+
guessed_letters = set()
92+
max_wrong = len(hangman_art) - 1
93+
94+
while True:
95+
display_man(wrong_guesses)
96+
display_hint(hint)
97+
98+
guess = input("Enter a letter: ").lower()
99+
100+
if not guess.isalpha() or len(guess) != 1:
101+
print("Please enter a single letter.")
102+
continue
103+
104+
if guess in guessed_letters:
105+
print("You already guessed that letter.")
106+
continue
107+
108+
guessed_letters.add(guess)
109+
110+
if guess in answer:
111+
for i, letter in enumerate(answer):
112+
if letter == guess:
113+
hint[i] = guess
114+
if "_" not in hint:
115+
print("🎉 You win! The word was:", answer)
116+
break
117+
else:
118+
wrong_guesses += 1
119+
if wrong_guesses == max_wrong:
120+
display_man(wrong_guesses)
121+
print("💀 You lost!")
122+
display_answer(answer)
123+
break
124+
125+
126+
if __name__ == "__main__":
127+
main()

45-hangman-game/words_list.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# generate a list of words for the hangman game, may be 200 words general knowledge, no computer terms
2+
words = (
3+
"apple", "orange", "banana", "grape", "kiwi", "mango", "peach", "pear", "plum", "cherry",
4+
"carrot", "broccoli", "spinach", "potato", "tomato", "cucumber", "lettuce", "onion", "pepper", "zucchini",
5+
"elephant", "giraffe", "kangaroo", "dolphin", "penguin", "tiger", "lion", "zebra", "monkey", "bear",
6+
"soccer", "basketball", "tennis", "cricket", "baseball", "hockey", "golf", "volleyball", "swimming", "cycling",
7+
"piano", "guitar", "violin", "drums", "flute", "trumpet", "saxophone", "cello", "harp", "accordion",
8+
"red", "blue", "green", "yellow", "purple", "orange", "pink", "brown", "black", "white",
9+
"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december")
-1.48 KB
Binary file not shown.
-1.71 KB
Binary file not shown.
-912 Bytes
Binary file not shown.
-864 Bytes
Binary file not shown.
-639 Bytes
Binary file not shown.
-1020 Bytes
Binary file not shown.
-716 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)