what have i written wrong?


Nobody just knows what's wrong, we all have to reason about what's going on in order to find what needs to be changed.

Start by identifying what's wrong about the result. Test it yourself if it's not apparent from the error message provided by codecademy.


what s wrong in this?
def censor(text,word):
for w in i:
if w==word:

return rec.append(w)

print censor("My name is Khan","Khan")



I'm not sure, but it seems like the problem may be the additional space you're adding in your else statement. Since you're joining all the indices of the index together with a " ", there's no need for that space. From what I understand.



It seems you may went to review some earlier lessons and that is not a bad thing, like anything worth knowing it takes time to understand programming. Rather than give you the answer I'll run through your code line by line with comments, to try and give you an understanding of what you're doing.

def censor(text,word): <---- doing everything right here, simply declaring a function with two params
rec=[] <--- again this is just fine, you're simply creating an empty list
i=text.split() <--- this is fine, but an unnecessary step to declare a variable equal to the list that is created by calling split on the text string.
for w in i: <--- Fine again, just iterating through a list, but you could combine this line with the previous and make it:
for w in text.split(): <--- which automatically creates a list, but again you're doing fine here
if w==word <--- again fine, simply creating a conditional
w=''len(word) <--- this is where the error begins occurring, and it's unclear what you're trying to do, but this simply isn't valid syntax. len(word) will return an integer equivalent to the length of characters in the word argument being passed and putting two quotations before it, is causing a syntax error. Removing the quotes, to get rid of the syntax error, we're left w = length(word passed as argument). For example len("hello) = 5, so w would be 5. You're then returning (which will cause a function to stop) the value of w appended to the empty list rec. If you're passing censor("My name is Khan", "Khan"), the function would return 4, since Khan is in the text argument.

Again, I think it would be beneficial for you to go back through some of the earlier courses. Simply progressing to the next course without learning the concept is pointless.

Best of luck in your future programming!


s,understood my mistake,thnks


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.