import random |
# list of words to choose from for the game |
words = [ "hangman" , "chairs" , "backpack" , "bodywash" , "clothing" , |
"computer" , "python" , "program" , "glasses" , "sweatshirt" , |
"sweatpants" , "mattress" , "friends" , "clocks" , "biology" , |
"algebra" , "suitcase" , "knives" , "ninja" , "shampoo" ] |
# choose a random word from the list |
word = random.choice(words) |
# create a list of dashes the same length as the word to be guessed |
# this will be used to track the player's progress |
dashes = [ "_" ] * len (word) |
# keep track of the player's lives |
lives = 8 |
# main game loop |
while lives > 0 : |
# print the current state of the game |
print ( "Word: " + " " .join(dashes)) |
print ( "Lives: " + str (lives)) |
# ask the player for a letter |
letter = input ( "Guess a letter: " ) |
# check if the letter is in the word |
if letter in word: |
# if it is, find all instances of the letter in the word |
for i in range ( len (word)): |
if word[i] = = letter: |
# replace the corresponding dash with the correct letter |
dashes[i] = letter |
else : |
# if the letter is not in the word, the player loses a life |
lives - = 1 |
# check if the player has won or lost |
if lives > 0 : |
print ( "You won! The word was " + word) |
else : |
print ( "You lost! The word was " + word) |