Lesson 13: Hangman revised post class

Post date: Jan 25, 2018 11:27:36 PM

#main.py

import hangman

print("Let's play Hangman")

word = hangman.getWord()

good_letters = ""

missed_letters = ""

missedGuess = 0

word_dash = hangman.getDashedWord(word, good_letters)

safe = False

hanged = False

while safe == False and hanged == False:

searchspace = hangman.wordLength(len(word))

searchspace = hangman.eliminateMissed(searchspace, missed_letters)

print(searchspace)

print(len(searchspace))

print(word_dash)

guess = input("Guess a letter:")

if word.find(guess) > -1 and good_letters.find(guess) == -1:

print("Correct guess!")

good_letters += guess

word_dash = hangman.getDashedWord(word, good_letters)

if word_dash.find("_") == -1:

safe = True

else:

print("Wrong guess!")

missedGuess += 1

if missed_letters.find(guess) == -1:

missed_letters += guess

hanged = hangman.drawHangman(missedGuess)

print("The correct word is ", word)

if hanged:

print("Sorry you are hanged...")

else:

print("You are safe!!!")

import random

def wordLength(length):

searchspace = []

for w in db:

if len(w) == length:

searchspace.append(w)

return searchspace

def eliminateMissed(words, missed_letters):

searchspace = []

for w in words:

for y in missed_letters:

if w.find(y) != -1:

searchspace.append(w)

return searchspace

def getWord():

select = random.randint(0,len(db))

return db[select]

def getDashedWord(word, good_letters):

word_dash = ""

for x in range(0, len(word)):

if good_letters.find(word[x]) > -1:

word_dash += word[x] + " "

else:

word_dash += "_ "

return word_dash

def drawHangman(missedGuesses):

if missedGuesses <= 0: return

print(" _____________")

if missedGuesses > 1:

print(" | |")

else: print(" |")

if missedGuesses > 2:

print(" | O")

else: print(" |")

if missedGuesses > 3:

print(" | /|\\")

else: print(" |")

if missedGuesses > 4:

print(" | |")

else: print(" |")

if missedGuesses > 5 :

print(" | / \\")

else: print(" |")

print(" |")

print(" |")

print(" |_____________")

return bool(missedGuesses > 6)