Lesson 10: Hangman v2

posted Dec 22, 2017, 7:50 AM by Attila Lengyel

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)

while word_dash.find("_") != -1:
  guess = input("Guess a letter:")
  if word.find(guess) > -1:
    print("Correct guess!")
    good_letters += guess
  else:
    print("Wrong guess!")
  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)

Comments