From 2d48eeb6ddde58736b7b294ffa54f99002836695 Mon Sep 17 00:00:00 2001 From: Ken Schaefer Date: Mon, 15 Jul 2024 18:02:10 -0500 Subject: [PATCH] day 7 --- 07-hangman/hangman.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/07-hangman/hangman.py b/07-hangman/hangman.py index b78e300..efb1edf 100644 --- a/07-hangman/hangman.py +++ b/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) \ No newline at end of file