Censor probleme


#1

Hallo, my english is very poor... . So , a I have to copy this code from https://external.codecademy.com/forum_questions/53cda358631fe9dea1000086 , with idle console to come this , this **** is wack **** , but with codecademy to come this... , this **** is wack hack

       
      def censor(text, word):
          text = ("this hack is wack hack")
          text = text.split(" ")
          for i in range(0, len(word):
            if text[i] == word:
                text[i] = "*" * len(word)
            return " ".join(text)

print censor("text", "hack")


#2

You should complete the bracket in the above quoted.

Also, please indent properly as this language requires so.

Your code if well formatted should look like this:

def censor(text, word):
    text = "this hack is wack hack"
    text = text.split(" ")
    for i in range(0, len(word)):
        if text[i] == word:
            text[i] = "*" * len(word)
            return " ".join(text)
print censor("text", "hack")

JUST TO SHARE. There is another way to go around this:

def censor(text,word):
    text=text.replace(word,'*' * len(word))
    return text
print censor("this hack is wack hack", "hack")

#3

Thanks for you answer , but I have identical as you has to write to me . I have your Text copy and paste to console from codecademy and I come identical answer .... this **** is wack hack


#4

[quote="bayoishola20, post:2, topic:41802"]
another way
[/quot

I have you another way to write , very thanks ! , what I hope anybody answer my asked , because I am three days that I to look for a solution to it ....


#5

@ilargi,
You will have to use the return-statement AFTER you have run through the complete FOR in loop
like

def censor(text, word):
    text = ("this hack is wack hack")
    text = text.split(" ")
    for i in range(0, len(text)):
        if text[i] == word:
            print word
            text[i] = "*" * len(word)
    return " ".join(text)

print censor("text", "hack")

#6

Thanks for your answer , but I have to write identical as you tell me , only that I have here different because I am second time that I write here .... I write to idle console :

          def censor(text, word):
text = ("This hack is wack hack")
text = text.split(" ")
for i in range(0, len(word)):
    if text[i] == word:
        text[i] = "*" * len(word)
return " ".join(text)

print censor("text", "hack")


#7

My bad, you were meant to loop in the for i in range(0, len(text)): and not for i in range(0, len(word)): as you did.

def censor(text, word):
    text = "this hack is wack hack";
    text = text.split(" ")
    for i in range(0, len(text)):
        if text[i] == word:
            text[i] = "*" * len(word)
    return " ".join(text)
print censor("text", "hack")

This should now give you:

this **** is wack ****

Hope this now resolves! :smile:


#8

Mila esker! ( of Basque Language , Very Thanks!) , now in console come this **** is wack **** , but too "

Oops,
try
again. Your function fails on censor("hey hey
hey","hey").
It returns "This hack is wack hack"
when
it should return "*** *** ***".

But not problem.. , I have to write to idle console and all it is gut .I think that problem is with codecademy console .... .I will a Day write as you short answer , but I will first understand the Python logic , I have begin with Python in January , I am total beginner in Python and I am little Old .. , too I working as scourer in Restaurants and i have only in Weekend Time to learn ... but I hope a Day and not in 10 Years ... , I have you knew in Python !


#9

@ilargi
The problem lies in the fact that
with
text = "this hack is wack hack"
you will re-assign any argument given as the censor()-function is called....
like
censor("hey hey","hey")

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".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#10

Hello , veryyyy Thanks for your help ! . I have understand what you mean .Because I have to write:
text = "this hack is wack hack" to the Function , now I have two Values that with old return-statement not going ....That problem is , how i answer ... I have to write :

  
def censor(text, word):
     texts = ("This hack is wack hack")
     text = text.split(" ")
     for i in range(0, len(text)):
        if text[i] == word:
             text[i] = "*" * len(word)
             return " ".join(text)
theResult = text("texts") 
print theResult("texts")

But I come this , TypeError: 'str' object is not callable


#11
def censor(text,word):
    text=text.replace(word,'*' * len(word))
    return text
print censor("this hack is wack hack", "hack")

Thank you so much! I spent the last two hours banging my head against the wall / my desk trying to figure this out without cheating.....I gave up though :frowning:

To those more experienced with programming, do you find it more beneficial to find the answer online or figure it out yourself?


#12

Hallo, very thanks for you answer and the time your give to this Question !
For me is more beneficial to find the answer online , because my python know is veryyyy slow ... but I not only copy and paste , i learn and asked to me , because it is so .So later ,when come another similar question , I know , what i should to do .... I am alone , and until now outside here , nobody have help me ....