-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman2.py
More file actions
136 lines (127 loc) · 3.64 KB
/
Copy pathhangman2.py
File metadata and controls
136 lines (127 loc) · 3.64 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
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
import random
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''','''
+----+
| |
[O |
/|\ |
/ \ |
|
==========''', '''
+----+
| |
[O] |
/|\ |
/ \ |
|
==========''']
words = {'color':'red orange yellow green blue indigo violet white black brown'.split(),
'shape':'square triangle rectangle circle ellipse rhombus trapezoid chevron pentagon hexagon septagon octagon'.split(),
'fruit':'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantaloupe mango strawberry tomato'.split(),
'animal':'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()}
def getSecretWord(wordDictionary):
secretKey = random.choice(list(wordDictionary.keys()))
secretWord = random.choice(wordDictionary[secretKey])
return secretWord
def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
print(HANGMANPICS[len(missedLetters)] + "\n")
print("Missed Letters: " + missedLetters + "\n")
guess = ""
for letter in secretWord:
if letter in correctLetters:
guess += letter
else:
guess += "_"
print(guess + "\n")
def guessLetter(secretWord, missedLetters, correctLetters):
guessedLetter = ""
while True:
guessedLetter = raw_input("Guess a letter.\n")
guessedLetter = guessedLetter.lower()
if guessedLetter in (missedLetters + correctLetters):
print ("Please enter a letter that hasn't been entered")
elif len(guessedLetter) != 1:
print ("Please enter only one letter")
elif not guessedLetter.isalpha():
print("Please enter a letter in the alphabet")
else:
return guessedLetter
break
missed, correct, secret, gameIsDone = ["","", getSecretWord(words), False]
print("HANGMAN")
while True:
for key in list(words.keys()):
if secret in words[key]:
if key == "animal": print("You are guessing an " + key)
else: print("You are guessing a " + key)
displayBoard(HANGMANPICS, missed, correct, secret)
guess = guessLetter(secret, missed, correct)
if(guess in secret):
correct = correct + guess
foundAllLetters = True
for letter in secret:
if letter not in correct:
foundAllLetters = False
break
if foundAllLetters:
gameIsDone = True
print("Yes! The secret word is " + secret + "! You have won")
else:
missed = missed + guess
if len(missed) > 7:
print('You have run out of guesses!\nAfter ' + str(len(missed)) + ' missed guesses and ' + str(len(correct)) + ' correct guesses, the word was "' + secret + '"')
gameIsDone = True
if gameIsDone:
replay = raw_input("Would you like to play again? (yes or no)\n")
if replay == "yes":
missed, correct, secret, gameIsDone = ["","", getSecretWord(words), False]
else:
break