Small troubles


#1

So, in “censor,” the tip is to use string.split() and " ".join(list) , however, not only do I not remember how these work, but I can’t seem to find them when I go through the previous lessons, so, if you can inform me which lessons they are discussed in, that would be greatly appreciated.

Also, because I’m pretty stubborn, I tried to code it without those functions, but it seems to have some problems.

Code
def censor(text,word):
  a=len(word)
  result=""
  z=0
  for char in text:
    if z>0:
      z-=1
    elif char==word[0]:
      b=word.index(char)
      if text[b:b+a]==word:
[details="Summary"]
This text will be hidden
[/details]

        result+="*"*a
        z=a-1
      #else: 
       # result+=char
    else:
      result+=char
  return result

[edit]
Nevermind the problems, I found out what was wrong. I really stupidly took the index of the character in the word, instead of in the phrase, and since almost all the phrase just so happened to start with the word to be censored, it always gave a positive. I still haven’t found the lesson where they discuss those functions, however, so that would still be appreciated.


#2

python also has documentation? A simple google search on python split and python join gives plenty of examples and explanations, without those function the solution becomes a lot more complicated.

it can be a fun challenge to attempt it without those function. There are no comments in your code, how do you detect the different words in text?


#3

Yeah, I thought about just googling it, but… I thought that maybe there would also be an answer the problems in the code without the functions.
If you managed to see my comment before I just edited it saying that I fixed it, I explained that I had it look for the first character in the word to be censored, then took a slice out of the text of the same length as the word, and checked to see if it was the same as the word.
Unless you mean “word” as like, things seperated by spaces. In which case, I didn’t. ("they", "hey") would get censored to t***.


#4

That depends on the purpose, but i would say they should be uncensored.

you could loop over text, and look for spaces (" "), this way you can create a list of the different words in the text string. This is also how split works.

this forum also has a censor function, but pass is allowed while ■■■ is not allowed. And hello is allowed while ■■■■ is not allowed


#5

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