· 7 years ago · Jan 17, 2019, 06:14 AM
1#Hangman
2
3import random
4import time
5HANGMANPICS = ['''
6
7 +---+
8 | |
9 |
10 |
11 |
12 |
13=========''', '''
14
15 +---+
16 | |
17 O |
18 |
19 |
20 |
21=========''', '''
22
23 +---+
24 | |
25 O |
26 | |
27 |
28 |
29=========''', '''
30
31 +---+
32 | |
33 O |
34 |\\ |
35 |
36 |
37=========''', '''
38
39 +---+
40 | |
41 O |
42 /|\\ |
43 |
44 |
45=========''', '''
46
47 +---+
48 | |
49 O |
50 /|\\ |
51 / |
52 |
53=========''', '''
54
55 +---+
56 | |
57 O |
58 /|\\ |
59 / \\ |
60 |
61=========''', '''
62
63 +---+
64 | |
65 [O |
66 /|\\ |
67 / \\ |
68 |
69=========''', '''
70
71 +---+
72 | |
73 [O] |
74 /|\\ |
75 / \\ |
76 |
77=========''']
78
79words = {'Colors':'red orange yellow blue green indigo violet white black brown' .split(),
80 'Shapes':'square triangle rectangle circle ellipse rhombus trapezoid chevron pentagon hexagon septagon octagon'.split(),
81 'Fruits':'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantaloupe mango strawberry tomato'.split(),
82 'Animals':'bat bear beaver cat cougar crab deer dog donkey duck eagle fish frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat zebra'.split()}
83
84def getRandomWord(wordDict):
85 #This function returns a random string from the dictionary
86 #First, selct the key
87 wordKey = random.choice(list(wordDict.keys()))
88
89 #Second it selects a word from the key's list
90 wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
91
92 return [wordDict[wordKey][wordIndex], wordKey]
93
94def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
95 print(HANGMANPICS[len(missedLetters)])
96 print()
97
98 print('Missed letters:', end= ' ')
99 for letter in missedLetters:
100 print(letter, end= ' ')
101 print()
102
103 blanks = '_' * len(secretWord)
104
105 for i in range(len(secretWord)): #replace blanks with correct letters
106 if secretWord[i] in correctLetters:
107 blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
108
109 for letter in blanks: #show secret word with blanks in between correct letters
110 print(letter, end= ' ')
111 print()
112
113def getGuess(alreadyGuessed):
114 #returns the letter the player guessed, and makes sure they guessed a letter
115 while True:
116 print('Take a.')
117 guess = input()
118 guess = guess.lower()
119 if len(guess) != 1:
120 print('Guess a SINGLE letter.')
121 time.sleep(1)
122 print()
123 elif guess in alreadyGuessed:
124 print('You already tried that one, try something different.')
125 time.sleep(1)
126 print()
127 elif guess not in 'abcdefghijklmnopqrstuvwxyz':
128 print('Guess a LETTER.')
129 time.sleep(1)
130 print()
131 else:
132 return guess
133
134def playAgain():
135 #This function returns True if the player wants to play again. Otherwise it returns false
136 print('Do you want to play again? (Yes or No)')
137 return input().lower().startswith('y')
138
139print('H A N G M A N')
140missedLetters = ''
141correctLetters = ''
142secretWord, secretKey = getRandomWord(words)
143gameIsDone = False
144
145while True:
146 print('The secret word is in category: ' + secretKey)
147 displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
148
149 #Let the player guess a letter.
150 guess = getGuess(missedLetters + correctLetters)
151
152 if guess in secretWord:
153 correctLetters = correctLetters + guess
154
155 #Check if player guessed all the letters
156 foundAllLetters = True
157 for i in range(len(secretWord)):
158 if secretWord[i] not in correctLetters:
159 foundAllLetters = False
160 break
161 if foundAllLetters:
162 print('Yes! The secret word was ' + secretWord)
163 gameIsDone = True
164
165 else:
166 missedLetters = missedLetters + guess
167
168 #check if player has taken too many guessed and lost
169 if len(missedLetters) == len(HANGMANPICS) - 1:
170 displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
171 print('You have run out of guesses! The secret word was ' + secretWord)
172 gameIsDone = True
173
174 if gameIsDone:
175 if playAgain():
176 missedLetters = ''
177 correctLetters = ''
178 gameIsDone = False
179 secretWord, secretKey = getRandomWord(words)
180 else:
181 break