Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assignment Help
#1
Hello, i've been asked in my assignment to create a censor function that replaces words in a text string with astricks of the same lenght if the word is in a banned list. I have wriiten a fucntion i thought would work however i cant seem to get it correct, it keeps failing the validations. Id appreciate any help at all! i just want to learn what im doing wrong with this so i can fix it. Thanks so much in advance! ill insert the full question and my answer that has failed validatons below.

Write a function censor which takes as input a string text and a list of strings banned. Your function should return a string which is obtained from text by replacing each occurrence of any string from banned in text by the string consisting of the same numbers of asterisks (*).

For example:

censor('This is appaling and outrageous.', ['appaling', 'outrageous']) should return 'This is ******** and **********.'

censor('I strongly dislike this problem.', ['dislike', 'hate']) should return 'I strongly ******* this problem.'

Note. You may assume that no string in banned occurs as a substring of any other string in banned.



def censor(text, banned):
    for i in banned:
        if i in text:
            text = text.replace(i,'*'*len(i), len(text.split()))
        return text
when i run the function with an input, the output only censors 1 word from the list banned even if there are more than one.
[in] censor('hello i am h', ['i', 'am'])
[out]'hello * am h'
Reply


Messages In This Thread
Assignment Help - by HNiChuimin - Nov-08-2020, 10:44 PM
RE: Assignment Help - by jefsummers - Nov-08-2020, 11:00 PM
RE: Assignment Help - by HNiChuimin - Nov-08-2020, 11:06 PM
RE: Assignment Help - by perfringo - Nov-09-2020, 05:16 AM
RE: Assignment Help - by Pedroski55 - Jan-07-2025, 05:05 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020