Lesson 13: Hangman v1.0 - Ready to play

posted Jan 26, 2018, 8:01 AM by Attila Lengyel
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("      |_____________")
 
import word_dict, random

size = len(word_dict.db)
print("Our database has", size, "words")

print("Let's play Hangman")
wordindex = random.randint(0, size)
word = word_dict.db[wordindex]

good_letters = ""
word_dash = ""
for x in range(0, len(word)):
  word_dash += "_ "
print (word_dash)

missed_guesses = 0

while missed_guesses < 7 and word_dash.find("_") != -1:
  guess = input("Guess a letter:")
  if word.find(guess) > -1:
    print("Correct guess!")
    good_letters += guess
  else:
    print("Wrong guess!")
    missed_guesses = missed_guesses + 1
    drawhangman(missed_guesses)
  word_dash = ""
  for x in range(0, len(word)):
    if good_letters.find(word[x]) > -1:
      word_dash += word[x] + " "
    else:
      word_dash += "_ "
  print(word_dash)

print("The word is", word)

Comments