Censor 10, help


#1

def censor(text, word):
word.split(word)
for x in word:
word = "*".join(word)
return word

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "h****e*******y" when it should return " *** ***".

Can someone tell me what im doing wrong?


#2

@petras98,

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#3

    words = text.split()

will split the phrase on each space character to give a list of words.

In the for loop you will need to compare each of the words to word, and replace any that match.

    result = []
    for x in words:
        if x == word:
            result.append('*' * len(word))
        else:
            result.append(x)
    return " ".join(result)

#4

@petras98,
From

[quote]

string.split(s[, sep[, maxsplit]])

Return a list of the words of the string s.

If the optional second argument sep is absent or None,
     the words are separated by arbitrary strings of whitespace characters 
     (space, tab, newline, return, formfeed). 
If the second argument sep is present and not None, 
    it specifies a string to be used as the word separator.
    The returned list will then have 
         one more item than the number of non-overlapping occurrences
         of the separator in the string.

If maxsplit is given, 
        at most maxsplit number of splits occur,
        and the remainder of the string is returned 
             as the final element of the list
        (thus, the list will have at most maxsplit+1 elements). 
If maxsplit is not specified or -1, 
         then there is no limit on the number of splits 
         (all possible splits are made).

The behavior of split on an empty string depends on the value of sep.
If sep is not specified, or specified as None,
            the result will be an empty list. 
If sep is specified as any string,
             the result will be a list containing one element 
             which is an empty string.

[end-quote]

def censor(text, word):
    #4 space indent
    # Replace all found =word= in =text= with ****
    print( "The word which should be replaced with **** : {0}".format(word) )
    #split the text into a =list= of words
    words=text.split()
    print("words-list is {0}".format(words))
    for x in words:
        #2nd 4 space indent
        #iterate over the =words-list=
        # every iteration the x-VARIABLE 
        #        will be the next Element of words-list
        print x
        #you have to find a way to replace the found =x=word with ***
        #word = "*".join(words)
        #return word
        
censor("This is this text this","this")

== guidance ==
http://legacy.python.org/dev/peps/pep-0008/
http://docs.python.org/2/reference/expressions.html
http://docs.python.org/2/library/functions.html