public
Created

this is a version of hangman for the mooc course, a gentle intro to python.

  • Download Gist
hangman
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
def print_hangman_image(mistakes = 6):
"""Prints out a gallows image for hangman. The image printed depends on
the number of mistakes (0-6)."""
if mistakes <= 0:
print''' ____________________
| .__________________|
| | / /
| |/ /
| | /
| |/
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| | | |
: : : :
. . . .'''
 
elif mistakes == 1:
print''' ___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \\
| | || `/,|
| | (\\\\`_.'
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| | | |
: : : :
. . . .'''
elif mistakes == 2:
print''' ___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \\
| | || `/,|
| | (\\\\`_.'
| | -`--'
| | |. .|
| | | |
| | | . |
| | | |
| | || ||
| |
| |
| |
| |
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| | | |
: : : :
. . . .'''
elif mistakes == 3:
print''' ___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \\
| | || `/,|
| | (\\\\`_.'
| | .-`--'
| | /Y . .|
| | // | |
| | // | . |
| | ') | |
| | || ||
| |
| |
| |
| |
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| | | |
: : : :
. . . .'''
elif mistakes == 4:
print''' ___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \\
| | || `/,|
| | (\\\\`_.'
| | .-`--'.
| | /Y . . Y\\
| | // | | \\\\
| | // | . | \\\\
| | ') | | (`
| | || ||
| |
| |
| |
| |
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| | | |
: : : :
. . . .'''
elif mistakes == 5:
print''' ___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \\
| | || `/,|
| | (\\\\`_.'
| | .-`--'.
| | /Y . . Y\\
| | // | | \\\\
| | // | . | \\\\
| | ') | | (`
| | ||'||
| | ||
| | ||
| | ||
| | / |
""""""""""""""""""""""""|
|"|"""""""""""""""""""|"|
| | | |
: : : :
. . . .'''
else: #mistakes >= 6
print''' ___________.._______
| .__________))______|
| | / / ||
| |/ / ||
| | / ||.-''.
| |/ |/ _ \\
| | || `/,|
| | (\\\\`_.'
| | .-`--'.
| | /Y . . Y\\
| | // | | \\\\
| | // | . | \\\\
| | ') | | (`
| | ||'||
| | || ||
| | || ||
| | || ||
| | / | | \\
""""""""""|_`-' `-' |"""|
|"|"""""""\ \ '"|"|
| | \ \ | |
: : \ \ : :
. . `' . .'''
 
 
 
def Correct_letter(guessed_letter,secret_word,letters_guessed,mistakes):
if guessed_letter in secret_word:
print "correct"
return mistakes
else:
print "not in the word"
mistakes+=1
return mistakes
 
def Guessed_yet(guessed_letter,letters_guessed,mistakes):
if guessed_letter in letters_guessed:
print "You guessed this already."
mistakes+=1
return mistakes
else:
letters_guessed+=guessed_letter
return Correct_letter(guessed_letter,secret_word,letters_guessed,mistakes)
 
def Show_board(secret_word,character_list,guesses):
board=[]
for i in secret_word:
if i in letters_guessed:
board.append(i)
else:
board.append('-')
 
if join(board,'')==secret_word:
return "You win!"
else:
return join(board,' ')
 
def endgame(secret_word,character_list,guesses,mistakes):
if Show_board(secret_word,character_list,guesses)=="You win!":
print secret_word,'!!!!!'
guesses=100
return guesses
elif mistakes==6:
print "too many mistakes! sorry you lose"
guesses=100
return guesses
else:
return guesses
 
def get_random_word():
"""Returns a random word from the file word_list.txt in the same directory"""
input_file = open('word_list.txt','r')
word_list = [word.strip().lower() for word in input_file.readlines() if word.strip().isalpha() and len(word) > 4]
input_file.close()
return word_list[random.randint(0,len(word_list)-1)]
 
from string import *
import random
##from hangman_lib import *
 
secret_word=get_random_word()
letters_guessed=[]
mistakes=0
guesses=0
character_list=[]
count=0
print "Art created by sk"
print''
print secret_word, 'is the secret word'
print''
 
print Show_board(secret_word,character_list,guesses)
 
while mistakes<7 and guesses<100:
 
guesses+=1
print ''
print "Guess #", guesses
print ''
guessed_letter=raw_input("Guess a letter:").lower()
 
mistakes= Guessed_yet(guessed_letter,letters_guessed,mistakes)
print ''
 
print Show_board(secret_word,character_list,guesses)
 
guesses = endgame(secret_word,character_list,guesses,mistakes)
 
print_hangman_image(mistakes)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.