Censor


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/censor-?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

Your function fails on censor(“Yo go fro yo go”,“go”). It returns "yo fro yo " when it should return "Yo fro yo ".

<What do you expect to happen instead?>

I put extra print statements in my code just so I could understand what is being inputted and the results. My console output is this-

the password is spam
spam
the password is ****
hey hey hey
hey


Yo go fro yo go
go
yo ** fro yo **

Which I think is the right solution? Then why is it showing me an error?

def censor(text, word):
  
  print text
  print word
  
  #Make the text and word lowercase
  
  text=text.lower()
  word=word.lower()
  
  #Splitting the string into words
  
  word_arr=text.split(" ")
  
  #checking if the word exists in the array and replacing it if it does
  
  for index in range(len(word_arr)):
    
    if word==word_arr[index]:
      
      #genenrating censored word
      censor_word=""
      
      for number in range(len(word)):
        censor_word=censor_word+'*'
              
      word_arr[index]=censor_word
            
  print " ".join(word_arr)  
  return " ".join(word_arr)


#2

the error is caused by the fact that you use .lower(), we should just leave the input as it is. Its also what is causing the error

the first word of Yo go fro yo go is uppercase, so the exercise Yo to be returned. Converting to lowercase will cause a problem


#3

Thank you so much! I feel like a complete moron though, serves me right for not reading the error message properly, gah!


#4

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