-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
118 lines (96 loc) · 2.49 KB
/
hangman.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# This project implements Hangman as a CLI game.
import random
def get_word():
"""
This function will return a random word from a list of words.
"""
# Load the list of words.
with open("words.txt", "r") as file:
words = file.read().splitlines()
# Return a random word from the list.
return random.choice(words)
def get_word_string(word, guesses):
"""
This function will return a string of the word with the correct guesses.
"""
# Initialize the string.
string = ""
# Loop through the word.
for letter in word:
# Check if the letter has been guessed.
if letter in guesses:
# Add the letter to the string.
string += letter
else:
# Add a dash to the string.
string += "-"
# Return the string.
return string
def print_status(word, guesses, lives):
"""
This function will print the game status.
"""
# Print the word.
print("Word: " + get_word_string(word, guesses))
# Print the number of lives left.
print("Lives: " + str(lives))
def get_guess(guesses):
"""
This function will get the user's guess.
"""
# Get the user's guess.
guess = input("------\nGuess a letter: ")
# Check if the user has already guessed the letter.
if guess in guesses:
print("You've already guessed that letter.")
return get_guess(guesses)
# Return the guess.
return guess
def is_word_guessed(word, guesses):
"""
This function will check if the user has guessed all of the letters in the word.
"""
# Loop through the word.
for letter in word:
# Check if the letter has been guessed.
if letter not in guesses:
# The user has not guessed the letter.
return False
# The user has guessed all of the letters.
return True
def play_game():
"""
This function is the game loop.
It will run until the user wins or loses.
"""
# Initialize the game.
word = get_word()
guesses = []
lives = 10
# Run the game loop.
while lives > 0:
# Print the game status.
print_status(word, guesses, lives)
# Get the user's guess.
guess = get_guess(guesses)
# Check if the user guessed correctly.
if guess in word:
print("Correct!")
else:
print("Incorrect.")
lives -= 1
# Add the guess to the list of guesses.
guesses.append(guess)
# Check if the user has won.
if is_word_guessed(word, guesses):
print("You win!")
break
# Check if the user has lost.
if lives == 0:
print("You lose! The word was: " + word)
# Ask the user if they want to play again.
play_again = input("Play again? (y/n): ")
if play_again == "y":
play_game()
if __name__ == "__main__":
play_game()