10/15 - what is the role of count?


#1
    words = text.split()
    result = ''
    stars = '*' * len(word)
    count = 0
    for i in words:
        if i == word:
            words[count] = stars
        count += 1
    result =' '.join(words)

    return result

i dint get what does that words[count] do?


#2

we have a string we want to censor, so first we convert the string to a list, separating the sentence into words

then we need censor specific words, to achieve this, we need to update elements within the list. This is where count comes in, it acts as index, indexes we can use to manipulate a list, remember:

a_list = ['a', 'b', 'c']
a_list[0] = 'd'
print a_ilst

but if you use a for loop, you can also just use range() which produces a list of integers which so happen to match your indexes of your list, if done right


#3

which element? Didnt get that?


#4

things inside a list are called elements or values. In this case, the words of our sentence we want to censor


#5
def censor(text, word):
    words = text.split()
    stars = '*' * len(word)
    for i in words:
        if i == word:
            words = words.replace(stars, word)
    words = ' '.join(words)

    print(words)

censor("hey just get lost", "lost")

whats wrong in this?


#6

using .replace() is boring, it allows for a single line solutions.

.replace() is a method belonging to string, words variable is a list.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.