namingthingsiseasy,

I think what tripped you up here is that you iterated over the wrong object. In your second solution:


<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>

while in the correct solution:


<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>

The most important difference here is that in your code block, you iterate over the letters, ie. ‘a’, ‘a’, ‘r’, …, while in the second you iterate over the numerical indices of the string, 0, 1, 2, …. In this specific use case, it’s much easier to use the numerical indices - because you can see how the second code block is using position in two places - once to retrieve the letter from the solution, and then again to update the display when the if condition matches.

Usually we prefer iteration using the method you used in your solution. But in this case, it’s easier to just iterate by index because you’re retrieving the element from one string, and updating the same position in the other string. You have no way of knowing what position to modify in display unless you have the numerical position, so it’s much easier to iterate that way in this case.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines