Browse Source

day 7

master
Ken Schaefer 4 months ago
parent
commit
2d48eeb6dd
  1. 35
      07-hangman/hangman.py

35
07-hangman/hangman.py

@ -1,13 +1,34 @@
# Step 1
import random
# Step 1
word_list = ["aardvark", "baboon", "camel"]
#TODO-1 Randomly choose a word from the word_list and assign it to a variable
# called chosen_word
# Randomly choose a word from the word_list and assign it to a variable
# called chosen_word
# chosen_word = word_list[random.randint(1, len(word_list) - 1)]
chosen_word = random.choice(word_list)
print(chosen_word)
# Create an empty list called display. For each letter in the chosen_word,
# add a "_" to display
display = []
for _ in chosen_word:
display += "_"
print(display)
#TODO-2 Ask the user to guess a letter and assign their answer to a variable
# called guess. Make guess lowercase
# Ask the user to guess a letter and assign their answer to a variable
# called guess. Make guess lowercase
#TODO-3 Check if the letter the user guessed (guess) is one of the letters in
# the chosen_word
guess = input("Guess a letter: ").lower()
# Loop through each position in the chosen_word, if the letter matches then
# replace the _ with the letter
for pos in range(len(chosen_word)):
letter = chosen_word[pos]
if letter == guess:
display[pos] = letter
# Print display and you should see the letter in the correct position
print(display)
Loading…
Cancel
Save