-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
40 lines (32 loc) · 1.22 KB
/
hangman.py
File metadata and controls
40 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
# The list of words to choose from
word_list = ["python", "java", "javascript", "c++", "c#", "swift"]
# The word that the player is trying to guess
word = random.choice(word_list)
# The number of incorrect guesses the player has made
incorrect_guesses = 0
# The letters that the player has guessed so far
guessed_letters = []
# The number of allowed incorrect guesses
MAX_INCORRECT_GUESSES = 6
# The letters that make up the word
word_letters = set(word)
# The game loop
while len(word_letters) > 0 and incorrect_guesses < MAX_INCORRECT_GUESSES:
print("The word: " + " ".join(letter if letter in guessed_letters else "_" for letter in word))
print("Incorrect guesses: " + str(incorrect_guesses))
print("Guessed letters: " + " ".join(guessed_letters))
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You already guessed that letter.")
elif guess in word_letters:
word_letters.remove(guess)
guessed_letters.append(guess)
else:
incorrect_guesses += 1
guessed_letters.append(guess)
print()
if len(word_letters) == 0:
print("Congratulations, you won! The word was " + word)
else:
print("Sorry, you lost. The word was " + word)