-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman2_V1.py
217 lines (172 loc) · 5.67 KB
/
hangman2_V1.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#def guessWord():
# homework eliminate this as a function
#letter_guess = input("Guess a letter").lower()
#return letter_guess
'''
def checkGuess(letter_guess, letterList):
elcount = 0
letter_found = False
for l,v in letterList:
if letter_guess == l:
letter_found = True
letterList[elcount] = (letter_guess, True)
elcount += 1
if letter_found:
print("Yay {} is in the word.".format(letter_guess))
else:
print("Sorry fool")
'''
# take input list 'wordPule' and generate output list.
# for every tuple in wordPule we make a new tuple. if the
# letter matches, we changed the Boolean to True. if
# letter doesn't match, we leave Boolean alone
# 'x'
# ('l', True) -> ('l', True)
# ('l', False) -> ('l', False)
# ('x', True) -> ('x', True)
# ('x', False) -> ('x', True)
# HOMEWORK Turn into a list comprehension
def checkGuess(letter, wordPule):
#
# guess 'c'
#
# [ ('a', False), ('b', True), ('c', False), ('d', False), ('e', True)
# [ ('a', False), ('b', True), ('c', True), ('d', False), ('e', True)
#
#output = []
#for le,bo in wordPule:
# if letter.lower() == le:
# output.append((le, True))
# else:
# output.append((le, bo))
#def transform(t):
# if letter == t[0]:
# return (t[0],True)
# else:
# return t
#output = [ transform(t) for t in wordPule]
output = [(t[0],True) if letter == t[0] else t for t in wordPule]
#found = False
#for le, bo in wordPule:
# if letter.lower() == le:
# found = True
if letter in [le for le,bo in output]:
#if found == True:
print("Nice job {} in the word".format(letter))
else:
print("Letter not in the word")
return output
def presentWord(word):
# homework listOfStrings = []
# NOT: [<print, x=1, if <>: else:> for <something> in <something>]
# YES: [<expression: x*x, x[0]> for <something> in <something>]
#
# [expression for item in list]
# with an if statement, assign the displayed form of a letter to 'd'
# input is tuple t
list1 = [ le if bo else " _ " for le,bo in word]
print("".join(list1))
# print as "t _ s _ o"
# "t, _ ,s, _ ,o" "".join(list1)
#print()
#for t in word:
# print(t[0] if t[1] else " _ ", end='')
def createWordPule(word):
return [(letter,False) for letter in word]
def loopGame():
print("loopGame Run")
# Initialize word.
word = "michaelmos"
# wordPule initialized here
wordPule = createWordPule(word)
# Begin loop.
guessCount = 0
while True:
presentWord(wordPule)
wordPule = checkGuess(input("Guess a letter").lower(), wordPule)
if all([v for l,v in wordPule]): # Checks to see if the user won
print("YOU WIN")
break
elif guessCount >= (len(word) + 4):
print("YOU LOSE")
break
else:
guessCount += 1
def promptForGameContinue():
while True:
s = input("type 'yes' or 'no': ")
if s == "yes":
return True
elif s == "no":
return False
print(s + " is not an accepted answer.")
#if "y" != input('Type "y" to Continue').lower():
# break
def main():
while True:
if not promptForGameContinue():
break
loopGame()
# Type 'yes' or 'no':
# foo
# Type 'yes' or 'no':
# yes
# ...
# Type 'yet' or 'no' to play again:
# nope
# Type ';et' or 'no':
# yes
# ...
if __name__ == '__main__':
main()
#!=
'''
HIGHLEVEL
- Loop through game decision options
---- Accept entry of whether or not the person wants to play again
---- if yes:
-------- call loopGame()
---- else:
-------- end the game by breaking game decision loop
Def loopGame():
- Select a word
- Present word and gallows (should it go here?)
- Loop until player wins or loses:
---- Present word and body parts (should it go here also, or only?)
---- Accept guess of letter
---- Check if letter is in selected word
-------- If letter is in selected word
------------ If the player has guessed all of the letters in the word
---------------- Present the “You Win” message
---------------- break
-------- If letter is not in selected word
------------ If the player has guessed incorrectly 6 times
---------------- Present “Game Over” message
---------------- break
MIDDLE LEVEL
X Select a word
- From a table, select a list of words that have not been flagged as guessed
- Assign selected word to a list called "word"
- Split the word into tuples where the letter is the first element of the tuple
and "False" is the second element of the tuple.
- Present word and gallows (should it go here?)
- Print an em
- Loop until player wins or loses:
---- Present word and gallows (should it go here also, or only?)
---- Accept guess of letter
---- Check if letter is in selected word
-------- If letter is in selected word
------------ If the player has guessed all of the letters in the word
---------------- Present the “You Win” message
---------------- Present Do you want to play again message
------------ If the player has not guessed all of the letters in the word
---------------- Present all instances of letter in word, in their appropriate space
---------------- Accept guess of letter
-------- If letter is not in selected word
------------ If the player has guessed incorrectly 6 times
---------------- Present “Game Over” message
---------------- Present Do you want to play again message
------------ If the player has guessed incorrectly less than 6 times
---------------- Present next body part
---------------- Accept guess of letter
'''