forked from tiyd-python-2015-01/mystery-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystery_word_tdd.py
113 lines (86 loc) · 2.77 KB
/
mystery_word_tdd.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
import random
import re
with open("/usr/share/dict/words") as w:
word_list = w.readlines()
guessed = []
revealed = []
def random_word_assignment():
global random_word
random_word = random.choice(word_list).strip().lower()
return random_word
def game_intro(random_word):
print_statement = "The word has {} letters. You have 8 guesses.".format(
len(random_word), counter)
print(print_statement)
return print_statement
def change_count_guess(counter):
counter -= 1
guessed.append(guess)
print(counter_statement)
return counter
# def display_counter():
# print(counter_statement)
def correct_guess(guess, revealed):
revealed.append(guess)
print("Good job! " + counter_statement)
return revealed
def incorrect_guess(guess, counter):
counter -= 1
guessed.append(guess)
print("Nope. " + counter_statement)
return guessed
def display_correct_guesses(guess):
print(" ".join(char if char in revealed else "_" for char in random_word))
return (" ".join(char if char in revealed else "_" for char
in random_word))
def get_guess():
guess = input("What's your guess? ").lower()
if guess in re.search[a-z]:
return str(guess)
print(guess)
else:
print("{} is not a valid choice. Try again.".format(guess))
def guess_list():
print(guessed)
return guessed
def game_over():
play_again = input("You won! Do you want to play again? Y or N ")
if play_again == 'y':
game_intro()
counter = 8
else:
print("goodbye.")
counter = 8
game_intro(random_word_assignment())
while True:
while counter > 0 and len(random_word) != len(revealed):
guess = input("What's your guess? ")
counter_statement = "You have {} guesses left. '\n'".format(counter)
if len(guess) > 1:
guess = input("You can only guess one letter at a time.
Try again. ")
elif guess in random_word:
correct_guess(guess, revealed)
display_correct_guesses(guess)
elif guess not in random_word:
counter -= 1
incorrect_guess(guess, counter)
display_correct_guesses(guess)
print(guessed)
else:
game_over()
print("You have run out of guesses. The word is {}.".
format(random_word))
break
else:
if len(random_word) == len(revealed):
play_again = input("You won! Do you want to play again? Y or N ")
if play_again == 'y':
game_intro()
counter = 8
else:
break
else:
game_over()
break
# Still accepts non-letters as correct. Replay function not working.