Skip to content

Commit d6e1ef4

Browse files
authored
Add files via upload
1 parent 996dbe5 commit d6e1ef4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

HangMan.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import random
2+
3+
def hangman():
4+
word_list = ["python", "hangman", "programming", "game"]
5+
word = random.choice(word_list)
6+
word_length = len(word)
7+
lives = 6
8+
guessed_letters = []
9+
word_display = "_" * word_length
10+
11+
while lives > 0:
12+
print(word_display)
13+
guess = input("Guess a letter: ").lower()
14+
15+
if guess in guessed_letters:
16+
print("You already guessed that letter.")
17+
continue
18+
19+
guessed_letters.append(guess)
20+
21+
if guess in word:
22+
word_as_list = list(word_display)
23+
indices = [i for i, letter in enumerate(word) if letter == guess]
24+
for index in indices:
25+
word_as_list[index] = guess
26+
word_display = "".join(word_as_list)
27+
if "_" not in word_display:
28+
print("You win!")
29+
break
30+
else:
31+
lives -= 1
32+
print(f"Incorrect. You have {lives} lives left.")
33+
34+
if lives == 0:
35+
print("You lose! The word was", word)
36+
37+
hangman()

0 commit comments

Comments
 (0)