10. Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


This a problem I'm having quite often; the message is, "Did you create a function called [censor]? Your code threw a "global name 'token' is not defined" error."

Does text.split() automatically slice the string into individual words?
I'm used to the syntax "for i in word", but that's for characters; does something like the below work for words in a phrase?


def censor(text, word):
    # return text with the word you chose replaced by asterisks
    text = raw_input("Please enter a phrase")
    word = raw_input("Please enter a word to be replaced with asterisks")
    text.split()
    if token in text == word:
        replace = len(token) * "*" 
    text.join(text, replace)
    return censor


#2

if you want to prompt the user for input, do this outside the function and supply them as argument.

where do you define token? so far i see nowhere

split() cuts a string into a list, by default its separate on spaces


#3

Thank you -- I'll keep working on it :slight_smile:


#4

if you need more help, post an updated version of your code


#5

ok, fixed after a LOT of struggle.
Adding lots of comments and checking to see if each step evaluated True or False helped a little. But I'm still unclear on something.

text = raw_input("Please enter your text string: ")
word = raw_input("Please enter the word to censor: ")

def censor(text, word):
   
    token = len(word) * "*"
    # set variable "token" as asterisks the length of word
    
    split = text.split()  
    # split the string "text" into a list called split
    
    for item in range(len(split)):
    # loop through the list, item by item 

        if split[item] == word:
        # check if the item matches the variable "word",

            split[item] = token
            # if True, return token (the asterisks)
        
    return " ".join(split)
    # join the list back into a string


print censor(text, word)

It wouldn't work when I added a for loop like this:

for item in range(len(split)):
    if item == word:
        item = token

and I'm not understanding why I had to specify that it was an index in a list, rather than just an item in a list.


#6

because range() just generates a list of numbers:

def censor(text, word):
   
    token = len(word) * "*"
    # set variable "token" as asterisks the length of word
    
    split = text.split()  
    # split the string "text" into a list called split
    
    print range(len(split))
    # using range will give a list which matches indexes
    # from your split list
    for item in range(len(split)):
        # item will contain index
        print item
    # loop through the list, item by item 
        if split[item] == word:
        # check if the item matches the variable "word",

            split[item] = token
            # if True, return token (the asterisks)
        
    return " ".join(split)
    # join the list back into a string


print censor("this hack is wack hack", "hack")

then we look over the list generate by range, which matches with the indexes of split

Hope this helps


#7

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