-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
70 lines (53 loc) · 2.21 KB
/
hangman.py
File metadata and controls
70 lines (53 loc) · 2.21 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
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
# Python Game with Unit Tests
import unittest
from unittest.mock import patch
from io import StringIO
def start_game(player_name):
print(f"Welcome, {player_name}, to the Hangman Game!")
word_list = ["apple", "banana", "cherry", "grape", "watermelon"]
secret_word = random.choice(word_list)
attempts = 0
max_attempts = 6
guessed_letters = []
while True:
print("Guess the word:")
display_word = ""
for letter in secret_word:
if letter in guessed_letters:
display_word += letter
else:
display_word += "_ "
print(display_word)
guess = input("Guess a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Invalid input. Please guess a single letter.")
continue
if guess in guessed_letters:
print(f"You've already guessed '{guess}'. Try again.")
continue
guessed_letters.append(guess)
if guess not in secret_word:
attempts += 1
if attempts >= max_attempts:
print(f"Sorry, {player_name}! You've run out of attempts. The word was '{secret_word}'.")
break
if all(letter in guessed_letters for letter in secret_word):
print(f"Congratulations, {player_name}! You've guessed the word '{secret_word}'!")
break
class TestHangmanGame(unittest.TestCase):
@patch('builtins.input', side_effect=['a', 'b', 'c', 'd', 'e', 'f'])
def test_game_wins(self, mock_input):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
start_game("Test Player")
output = mock_stdout.getvalue()
self.assertIn("Congratulations, Test Player! You've guessed the word", output)
@patch('builtins.input', side_effect=['x', 'y', 'z', 'p', 'q', 'r'])
def test_game_loses(self, mock_input):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
start_game("Test Player")
output = mock_stdout.getvalue()
self.assertIn("Sorry, Test Player! You've run out of attempts.", output)
if __name__ == "__main__":
import random
name = input("Enter your name: ")
start_game(name)