-
Notifications
You must be signed in to change notification settings - Fork 0
/
letter_game.py
176 lines (158 loc) · 2.82 KB
/
letter_game.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import random
import sys
# make a list of words
words = [
'apple',
'apricot',
'avocado',
'banana',
'bilberry',
'blackberry',
'blackcurrant',
'blueberry',
'boysenberry',
'currant',
'cherry',
'cherimoya',
'cloudberry',
'coconut',
'cranberry',
'damson',
'date',
'dragonfruit',
'durian',
'elderberry',
'feijoa',
'fig',
'gooseberry',
'grape',
'raisin',
'grapefruit',
'guava',
'huckleberry',
'jabuticaba',
'jackfruit',
'jambul',
'jujube',
'kiwifruit',
'kumquat',
'lemon',
'lime',
'loquat',
'longan',
'lychee',
'mango',
'marionberry',
'melon',
'cantaloupe',
'honeydew',
'watermelon',
'mulberry',
'nectarine',
'nance',
'olive',
'orange',
'clementine',
'mandarine',
'tangerine',
'papaya',
'passionfruit',
'peach',
'pear',
'persimmon',
'physalis',
'plantain',
'plum',
'prune',
'pineapple',
'plumcot',
'pomegranate',
'pomelo',
'quince',
'raspberry',
'salmonberry',
'rambutan',
'redcurrant',
'salal berry',
'salak',
'satsuma',
'strawberry',
'tamarillo',
'tamarind',
'tomato',
'yuzu'
]
def clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def draw(bad_guesses, good_guesses, secret_word):
clear()
print('Strikes: {}/7'.format(len(bad_guesses)))
print('')
for letter in bad_guesses:
print(letter, end=' ')
print('\n\n')
for letter in secret_word:
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
def get_guess(bad_guesses, good_guesses):
while True:
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess one letter!")
elif guess in bad_guesses or guess in good_guesses:
print("Youve already guessed that letter!")
elif not guess.isalpha():
print("You can only guess letters!")
else:
return guess
def play(done):
clear()
secret_word = random.choice(words)
bad_guesses = []
good_guesses = []
while True:
draw(bad_guesses, good_guesses, secret_word)
guess = get_guess(bad_guesses, good_guesses)
if guess in secret_word:
good_guesses.append(guess)
found = True
for letter in secret_word:
if letter not in good_guesses:
found = False
if found:
print("You Win!")
print("The secret word was {}".format(secret_word))
done = True
else:
bad_guesses.append(guess)
if len(bad_guesses) == 7:
draw(bad_guesses, good_guesses, secret_word)
print("You Lost!")
print("The Secret word was {}".format(secret_word))
done = True
if done:
play_again = input("Play Again? Y/n ").lower()
if play_again != 'n':
return play(done=False)
else:
sys.exit()
def welcome():
start = input("Press enter/return to start or Q to quit").lower()
if start =='q':
print("Bye!")
sys.ext()
else:
return True
print('Welcome to Letter Guess!')
done = False
while True:
clear()
welcome()
play(done)