Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
this is a version of hangman for the mooc course, a gentle intro to python.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.