Skip to content

Commit ab5a617

Browse files
committed
Create Hangman game
1 parent 673de09 commit ab5a617

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Day007/Hangman/hangman.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import random;
2+
ALLOWED_FAILURES = 6;
3+
words = ["buffalo", "parrot", "squirrel", "hedgehog"];
4+
word = random.choice(words);
5+
6+
correct_letters = ['_'] * len(word);
7+
incorrect_letters = [];
8+
9+
letters_remaining = len(word);
10+
while letters_remaining > 0 and len(incorrect_letters) < ALLOWED_FAILURES:
11+
guess = input("Guess a letter: ").lower();
12+
if guess in correct_letters or guess in incorrect_letters:
13+
print("That letter has already been used.");
14+
continue;
15+
elif not guess.isalpha():
16+
print("That is not a letter.");
17+
continue;
18+
19+
i = 0;
20+
is_in_word = False;
21+
for letter in word:
22+
if guess == letter:
23+
is_in_word = True;
24+
correct_letters[i] = letter;
25+
letters_remaining -= 1;
26+
i += 1;
27+
28+
if not is_in_word:
29+
incorrect_letters.append(guess);
30+
31+
print(f"The word so far: {correct_letters}");
32+
print(f"Incorrect guesses: {incorrect_letters}");
33+
34+
print("Game Over");
35+
if len(incorrect_letters) < ALLOWED_FAILURES:
36+
print("Player wins!");
37+
else:
38+
print("Player loses :(");

0 commit comments

Comments
 (0)