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

Osnapitsjoey@lemmy.one to Programming@programming.dev – 31 points –

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:

import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)

#Testing code
print(f'Pssst, the solution is {chosen_word}.')

#Create an empty List called display.
#For each letter in the chosen_word, add a "_" to 'display'.
#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.


display = ["_"] * len(chosen_word)


guess = input("Guess a letter: ").lower()

#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].

for letter in chosen_word:
if guess == letter:
for i in range(len(chosen_word)):
display.insert(i, guess)

print(display)

second:

for letter in chosen_word:
  if guess == letter:
    for i in range(len(chosen_word[letter])):
      display.insert(i, guess)

I ended up just saying screw it and went to this:

display = []
for char in chosen_word:
    if guess == letter:
        display += letter
   else:
    display += "_"

correct way of doing it:

import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)

print(f'Pssst, the solution is {chosen_word}.')

display = []
word_length = len(chosen_word)
for _ in range(word_length):
  display += "_"
print(display)
  
guess = input("Guess a letter: ").lower()


for position in range(word_length):
  letter = chosen_word[position]
  if letter == guess:
    display[position] = letter

print(display)

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!

20

You are viewing a single comment

Don’t forget Python’s amazing list comprehension syntax!

guess = input(“Guess a letter:”).lower()
display = [ letter if letter == guess else “_” for letter in word ]

Just one part of your question, but it saves a lot of futzing around with indices and replaces.

Even as an experienced python dev I sometimes prefer explicit for loops over list comprehensions. I think for people who didn't even grasp the concept of a for loop they are more confusing than helping.

Yeah, I'm not a fan of them overall. I think multiple lines is more readable. There are a LOT of people that disagree with me though.

I thought I was the only one... to me unless it's a super-simplistic comprehension, it has a similar effect as when C programmers write if (xx = !(1 == (a ? !c : 34 ^ blit_target))) {. Congratulations, you fit it all on one line! At the expense of totally destroying my train of thought when I'm trying to scan down the code and figure out what the hell's going on. Well done.