need help with some fundamentals of for loops. it looks like im so close to fully grasping grabbing an item out of a list, but not quite. examples below
so ill post a few of my failed examples below along with what I came up with as a fix, and then the actual correct code. I feel like im so close to grasping this, but missing some logic. this is for a hangman game.
one of the failed attempts:
<span style="color:#323232;">import random
</span><span style="color:#323232;">word_list = ["aardvark", "baboon", "camel"]
</span><span style="color:#323232;">chosen_word = random.choice(word_list)
</span><span style="color:#323232;">
</span><span style="color:#323232;">#Testing code
</span><span style="color:#323232;">print(f'Pssst, the solution is {chosen_word}.')
</span><span style="color:#323232;">
</span><span style="color:#323232;">#Create an empty List called display.
</span><span style="color:#323232;">#For each letter in the chosen_word, add a "_" to 'display'.
</span><span style="color:#323232;">#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">display = ["_"] * len(chosen_word)
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">guess = input("Guess a letter: ").lower()
</span><span style="color:#323232;">
</span><span style="color:#323232;">#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
</span><span style="color:#323232;">#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].
</span><span style="color:#323232;">
</span><span style="color:#323232;">for letter in chosen_word:
</span><span style="color:#323232;">if guess == letter:
</span><span style="color:#323232;">for i in range(len(chosen_word)):
</span><span style="color:#323232;">display.insert(i, guess)
</span><span style="color:#323232;">
</span><span style="color:#323232;">print(display)
</span>
second:
<span style="color:#323232;">for letter in chosen_word:
</span><span style="color:#323232;"> if guess == letter:
</span><span style="color:#323232;"> for i in range(len(chosen_word[letter])):
</span><span style="color:#323232;"> display.insert(i, guess)
</span><span style="color:#323232;">
</span><span style="color:#323232;">I ended up just saying screw it and went to this:
</span><span style="color:#323232;">
</span><span style="color:#323232;">display = []
</span><span style="color:#323232;">for char in chosen_word:
</span><span style="color:#323232;"> if guess == letter:
</span><span style="color:#323232;"> display += letter
</span><span style="color:#323232;"> else:
</span><span style="color:#323232;"> display += "_"
</span>
correct way of doing it:
<span style="color:#323232;">import random
</span><span style="color:#323232;">word_list = ["aardvark", "baboon", "camel"]
</span><span style="color:#323232;">chosen_word = random.choice(word_list)
</span><span style="color:#323232;">
</span><span style="color:#323232;">print(f'Pssst, the solution is {chosen_word}.')
</span><span style="color:#323232;">
</span><span style="color:#323232;">display = []
</span><span style="color:#323232;">word_length = len(chosen_word)
</span><span style="color:#323232;">for _ in range(word_length):
</span><span style="color:#323232;"> display += "_"
</span><span style="color:#323232;">print(display)
</span><span style="color:#323232;">
</span><span style="color:#323232;">guess = input("Guess a letter: ").lower()
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">for position in range(word_length):
</span><span style="color:#323232;"> letter = chosen_word[position]
</span><span style="color:#323232;"> if letter == guess:
</span><span style="color:#323232;"> display[position] = letter
</span><span style="color:#323232;">
</span><span style="color:#323232;">print(display)
</span>
so as you can see, i get that I can grab specific parts of a list using indices or slices, but somewhere in my brain my logic is wrong. if you guys have struggled with this before or if you have a good youtube video to help me break it down id be beyond thankful!
Add comment