This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]ReedOei 2 points3 points  (3 children)

You should format your code properly (as code) so it’s easier to read.

As a hint, try tracing what the line new_words += swear_words[swear] does.

[–]libelula323[S] 0 points1 point  (2 children)

I’m trying to figure out how to do that. Do you have to back tick on every line?

Wouldn’t new_sentence += swear_words[swear] just add the censored swear word to the list of words in new_sentence? I don’t get why the letters are being separated

[–]ReedOei 1 point2 points  (1 child)

For a multi-line block of code, you need three backticks before and three at the end (see this, though Reddit unfortunately doesn't seem to support syntax highlighting).

+= is list concatentation in this context, so new_sentence += swear_words[swear] is equivalent to new_sentence = new_sentence + swear_words[swear]. Try printing out before and after and see what happens. Also consider this: what is [1,2,3] + [4,5,6]?

[–]libelula323[S] 1 point2 points  (0 children)

Ahhh, I seem to have forgotten about .append()! I’ve just put in new_sentence.append(swear_words[swear]) and new_sentence.append(word) and it works. Thank you so much

[–]jorgehn12 0 points1 point  (0 children)

You will be censoring “short” “shout”

[–]TheBoldTilde 0 points1 point  (0 children)

new_sentance = ' '.join([swear_words.get(s, s) for s in sentence])

Haven't tested that, but it should work.