Censor dispenser

i wanted to replace a list with a code’x’ by using for
here is my code:
#first i am giving the string
string1=“HELLO everyone i am a beginner in codecademy.I am practising python. I wanted help with censor dispensership”
#now the list of words i want to replace it with
list=[‘everyone’,‘beginner’,‘codecademy’,‘python’,‘I’]
#now i define a function
def censor1(y):
b=string1.replace(y,‘x’ )
return b
for i in list:
censoremail1=censor1(i)
print(censoremail1)
#this function returns me by censoring only ‘I’ .Is there any other way to solve this kind of problems if so could plz type it.Thx for your help.And tell me where i went wrong

Hi @rishikeshupadhya7842 - welcome to the forum.

I’m not going to give you the solution outright, but I will nudge you in the right direction.

Firstly, I’ve made an assumption about what your code should look like. It helps, especially with Python, if you use the code </> icon to retain the formatting of your program. Otherwise, the forum removes all the whitespace and we don’t know how you’ve indented things like your for loop.

Anyway, this is what I think you wrote...
#first i am giving the string
string1="HELLO everyone i am a beginner in codecademy.I am practising python. I wanted help with censor dispensership"
#now the list of words i want to replace it with
list=["everyone","beginner","codecademy","python","I"]
#now i define a function
def censor1(y):
    b=string1.replace(y,"x")
    return b
for i in list:
    censoremail1=censor1(i)
print(censoremail1)

The string.replace() method doesn’t change the string it’s acting on in situ, rather we need to assign a new variable to hold the new string with the appropriate replacements. You’re doing this in your code already: b=string1.replace(y,"x").

To figure out why your code isn’t behaving as intended, and only seemingly replacing I, ask yourself this:

Where are you keeping the updated value of string1 from each iteration of your for loop?

Hi All,

Having some trouble with this one as well, any help is appreciated as I’m lost. I’m on #3

Write a function that can censor not just a specific word or phrase from a body of text, but a whole list of words and phrases, and then return the text.

Mr. Cloudy has asked that you censor all words and phrases from the following list in email_two .

proprietary_terms = [“she”, “personality matrix”, “sense of self”, “self-preservation”, “learning algorithm”, “her”, “herself”]

How can I update value of string in a for loop?I tried looking it up in internet but i didn’t get it could you plz help!

Hey @rishikeshupadhya7842

Let’s assume, again, that this is your code:

#first i am giving the string
string1="HELLO everyone i am a beginner in codecademy.I am practising python. I wanted help with censor dispensership"
#now the list of words i want to replace it with
list=["everyone","beginner","codecademy","python","I"]
#now i define a function
def censor1(y):
    b=string1.replace(y,"x")
    return b
for i in list:
    censoremail1=censor1(i)
print(censoremail1)

Let’s step through your for loop, shall we?

First iteration - i = "everyone".
We run censoremail1 = censor1(i), which results in:
b = string1.replace("everyone") = "HELLO x i am a beginner in codecademy.I am practising python. I wanted help in censor dispensership."

So, we now have censoremail1 = "HELLO x i am a beginner in codecademy.I am practising python. I wanted help in censor dispensership."

Second iteration - i = "beginner".
We run censoremail1 = censor1(i) again, which results in:
b = string1.replace("beginner") = "HELLO everyone i am a x in codecademy.I am practising python. I wanted help in censor dispensership."

So, we now have censoremail1 = "HELLO everyone i am a x in codecademy.I am practising python. I wanted help in censor dispensership."

Third iteration - i = "codecademy".
Hopefully by now, it’s apparent what’s happening. Each iteration of the for is censoring the appropriate word, but we’re assigning this to a new random variable censoremail1 each time and re-using the original string1 in the subsequent iteration.

Now, we have censoremail1 = "HELLO everyone i am a beginner in x.I am practising python. I wanted help in censor dispensership."

Fourth iteration - i = "python".
The result of this iteration is censoremail1 = "HELLO everyone i am a beginner in codecademy.I am practising x. I wanted help in censor dispensership."

Fifth iteration - i = "I".
This is the final iteration of the loop, and we end up with:
censoremail1 = "HELLO everyone i am a beginner in codecademy.x am practising python. x wanted help in censor dispensership."

We now exit the for loop, and print the value of censoremail1 to the console:

HELLO everyone i am a beginner in codecademy.x am practising python. x wanted help in censor dispensership.

Can you figure out what you need to change to get the desired output, where each word is censored and the cumulative result of each iteration is printed to the console at the end?

The solution is below, blurred out. I would strongly encourage you to think about the problem and attempt to fix it yourself before checking it.

#first i am giving the string
string1="HELLO everyone i am a beginner in codecademy.I am practising python. I wanted help with censor dispensership"
#now the list of words i want to replace it with
list=["everyone","beginner","codecademy","python","I"]
#now i define a function
def censor1(y):
    b=string1.replace(y,"x")
    return b
for i in list:
    string1=censor1(i)
print(string1)
4 Likes

To piggyback off of @thepitycoder, I just started learning Python and found this site extremely useful . You input your code and it shows you every step the computer or interpreter goes through when executing the code. It gives you a clearer mental picture of what each line of your code does and what might be going wrong with it.

I also had a lot of trouble with this step and had to look elsewhere for advice!
Step 4 made me save my progress and step away for the day lol.