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

dneaves@lemmy.world ⁨1⁩ ⁨year⁩ ago

Based on the first example:

If you want to help yourself a bit, enumerate your for loop. enumerate turns an iterable (like a list, or in this case a string) into an iterable of tuples, with contents being an int representing the index an item and the item itself:

for (i, letter) in enumerate(chosen_word):

(Side note, the parenthesis surrounding (i, letter) are optional. I purposely included them to show that it’s a tuple.)

i will be the index of each character, and letter will be the character itself. You can then do:

if letter == guess:

And to wrap it up, do list assignment by index. Someone already mentioned why not to use insert in this scenario, so I won’t repeat them. The following will instead overwrite the item at display index i with the guessed character:

display[i] = guess

source
Sort:hotnewtop