0

I'm trying to build a function that when passed a letter and a word as an argument, returns a list of strings with this format: '' for each letter with exception of the given letter. So when passed "house" and "o" should return ["", "o","","","_"] .

The problem is when the letter appears more than one time.

def char_positioner(word, guessAttempt):
    listedWordBlanks = list(len(word) * '_')

    i = word.index(guessAttempt)

    if guessAttempt in word:
        listedWordBlanks[i] = word[i]


    return listedWordBlanks

This is my second attempt but still getting the same result:

word = ['rotten']

wordSpaces = len(word[0]) * '_'
listWordSpaces = list(wordSpaces)


def testPositioner(char):
    
    for space in listWordSpaces:
        if char in word[0]:
            for letter in word[0]:
                listWordSpaces[word[0].index(char)] = char


    return listWordSpaces


testPositioner('t')

Expected result: ['_','_','t','t','_','_',]
**Got:**['_','_','t','_','_','_'] 

3 Answers3

0

No need to use index. In the end you will have to iterate over all letters, so just go with a simple loop checking each letter if it matches the target letter:

word = "rotten"
res = ['_' for _ in range(len(word))]
target = 't'
for i, letter in enumerate(word):
    if letter == target:
        res[i] = target

At the end of this, res will be:

['_', '_', 't', 't', '_', '_']
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Hey thanks, can you please explain this line: `['_' for _ in range(len(str_word))]` (Is it a special for loop?) – Nicolas Poletti Sep 18 '20 at 05:43
  • @NicolasPoletti First of all it is a [list-comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) which I hope you are familiar with as it is a very useful tool in Python. – Tomerikoo Sep 18 '20 at 14:56
  • Second I just use the [throw-away variable](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) `_` because I don't really care for the index. This is basically equivalent to `['_'] * len(word)`. I don't use that way because it can create problems when dealing with nested lists (see [this question](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly)), so it's better not to get used to it in the first place. – Tomerikoo Sep 18 '20 at 14:56
0

The index() method of string types return the index of the first found element that matches the parameter you pass, a character in your case. That's why it always returns the first letter's index when it's a repeated one. Using your logic, I would modify it as follows:

if char in listWordSpaces:
    for index, letter in enumerate(word[0]):
        if letter == char:
            listWordSpaces[index] = char

As you can see, I used enumerate(), which returns a list of tuples of two elements: the index and the value of each element of the iterable you're iterating over. Then I just compare if the letter in the word is equal to the character and, if so, I put that character in its place in your list.

Shinra tensei
  • 1,283
  • 9
  • 21
0

I would recommend that you not check once against the entire word. Instead check for each position.

def char_positioner(word, guessAttempt):
    listedWordBlanks = list(len(word) * '_')

    for i, w in enumerate(word):

        if w == guessAttempt:
            listedWordBlanks[i] = w

    return listedWordBlanks

This will ensure that all occurrences of guessAttempt will be captured into listedWordBlanks.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33