Censor - hit problem with join syntax and also global function


#1



Can please help to look at where did i go wrong here. scratching my balding head with this little problem :slight_smile:

hits 2 errors when i try debugging


Oops, try again. Did you create a function called censor? Your code threw a "global name 'censor' is not defined" error.

File "python", line 7
return " ".join(i)
^
SyntaxError: invalid syntax


def censor(text,zap):
    text = str(raw_input("What's your       word?"))
    text = text.split()
    for i in text:
        if i == zap:
            i.replace("*" * len(zap)
return " ".join(i)


#2

Syntax error is, return outside of a function. It should line up with the for.


#3

You mean like this? Still hit the same error msg though

def censor(text,zap):
    text = str(raw_input("What's your       word?"))
    text = text.split()
    for i in text:
        if i == zap:
            i.replace("*" * len(zap)
    return " ".join(i)

File "python", line 7
return " ".join(i)
^
SyntaxError: invalid syntax


#4

Yes, like that. It is inside the code block.

Missing a closing parenthesis on that line.


#5

Ah yes, managed to solve that part of the problem. funny though, it kept pointing to line 7 even though my error is at line 6 :slight_smile:

But i still hit this problem. any idea why, since i thought i did define my function earlier?
Oops, try again. Did you create a function called censor? Your code threw a "local variable 'i' referenced before assignment" error.


#6

Python error messages place the carat at the point in the code where parsing stops. Often it will appear on the line after the error. If the line is not where the error is, then look back at the previous line.

That line is still missing a ).


#7

Is there a missing parenthesis? Sorry, but i don't see it

Oops, try again. Did you create a function called censor? Your code threw a "local variable 'i' referenced before assignment" error.

def censor(text,zap):
    text = str(raw_input("What's your word?"))
    text = text.split()
    for i in text:
        if i == zap:
            i.replace("*" * len(zap))
    return " ".join(i)

#8

One problem that stands out is the user input. That is not expected, nor requested. Your function takes a string as a parameter. We wouldn't want to ignore and overwrite it. The line should be removed.


#9

So i should take the user input outside the function? Okay, but now i hit another error, where below

Oops, try again. Does your censor function take exactly two arguments, a text string and a word to replace with asterisks? Your code threw a "replace() takes at least 2 arguments (1 given)" error.

def censor(text,zap):
    #text = str(raw_input("What's your word?"))
    text = text.split()
    for i in text:
        if i == zap:
            i.replace("*" * len(zap))
    return " ".join(i)

#10

Now we're cooking with gas. I have been waiting to finally get to this error.

The use of a built in function sort of skirts the aim of this exercise which is to find a step by step method. At this stage in the learning we are more focused upon programming concepts than Python built-ins. They come up later. Can you come up with a way to do this without using .replace?

We are the user. Simply pass the values in the function call and forget about user input.

print censor("to be or not to be", "be")

#11

you mean like this

def censor(text,zap):
    text = text.split()
    for i in text:
        if i == zap:
            i = "*" * len(zap)
        else:
            i = i
    return " ".join(i)

print censor("to be or not to be", "be")

now i hit another error .

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

Output is
* *
None

my guess is the loop did not finish running. i commented the Else statement, and still get the same problem


#12

See if this trial run doesn't give you a little insight...

>>> text = "to be or not to be".split()
>>> for i in text:
	print (text[text.index(i)])
	fill = "*" * len(i)
	print (fill)
	text[text.index(i)] = fill

	
to
**
be
**
or
**
not
***
to
**
be
**
>>> text
['**', '**', '**', '***', '**', '**']
>>>

Now for the final test...

>>> text = "to be or not to be".split()
>>> word = 'be'
>>> fill = "*" * len(word)
>>> for i in text:
	if i == word:
		text[text.index(i)] = fill

		
>>> text
['to', '**', 'or', 'not', 'to', '**']
>>>

#13

3 posts were split to a new topic: S there any way I can check if word is in the text?


#14

ah, yessss, finally. thank you so much. Have a great week ahead. :relaxed:

i finally realised my silly error. in joining the text, i used i, instead of using newtext. no wonder it keeps throwing back either to or be as the returned value

def censor(text,word):
    newtext = text.split()

    fill = "*" * len(word) 
    for i in newtext:
        if i == word:
            newtext[newtext.index(i)] = fill
    censoredtext = " ".join(newtext)
    return censoredtext

print censor("to be or not to be", "be")

#15

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