10. Censor - I can't keep all my whitespace


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096


I can't figure out how to keep my whitespace.
I have tried 2 different approaches (see below)
error message:
Your function fails on censor("hey hey hey","hey"). It returns "******" when it should return " *** ***".


def censor(text, word):
    splittet = text.split()
    for each in splittet:
        if each == word:
            stars = len(each) * '*'
            place = splittet.index(word)
            splittet.insert(place, stars)
            splittet.remove(word)
    return splittet


def censor(text, word):
    splittet = text.split()
    for a,b in enumerate(splittet):
        if b == word:
            stars = len(b) * '*'
            splittet[a] = stars
        else:
            splittet[a] = b
    return ''.join(splittet)


#2

BTW: if i use "bad coding" anywhere, please let me know. I don't want bad habits from the beginning.


#3

here:

return ''.join(splittet)

the character between the apostrophes is used to join the list elements together, so if you insert a space between the apostrophes you should be good

also, for your first approach you also need to use join


#4

oh.. thank you very much. it's the small things that matters in coding!

I know, please don't judge :smile:


#5

yep, as for bad practice, you have one here:

stars = len(each) * '*'

you only have to do this once, not every run of the loop


#6

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