Syntax error

I’m getting a syntax error for this

 else:
     email_num3.append(word)

I can’t figure our why

Post a link to the lesson along with the all the code in the script?

censor dispenser

def censor_list(email_num, to_censor):
  email_num2=email_num.replace('\n',' ' ).split(' ')
  email_num3=[]
  print(email_num2)
  for word in email_num2:
    for piece in to_censor:
        if word == piece:                  
            email_num3.append(email_num2.strip(word)
        
        else:
            email_num3.append(word)
  print(email_num3)

censor_list(email_two,["she", "personality matrix", 
"sense of self", "self-preservation",
 "learning algorithm", "her", "herself"])
      

Posting the error itself would help. Not sure why its complaining of a syntax error. I do find the space between your if and else odd. Python will complain about indentions. Is this an indention error its throwing at you? If so, remove the space.

Reason I mention that is that I don’t see a syntax error with .append.

# animals list
animals = ['cat', 'dog', 'rabbit']
# 'guinea pig' is appended to the animals list
animals.append('guinea pig')
# Updated animals list
print('Updated animals list: ', animals)

If I reindent this in my editor:
(no, python code can’t be automatically indented, but in some places there is enough information about where the next line should go (also, my indentation script kinda gives up, but that’s fine, it still went far enough that it made the problem clear))

Other ways you can find the problem is by holding down backspace until it’s gone and then considering what you last removed.

Or only insert things that make sense as a whole. Don’t insert half things.

After-the-fact you can still find it by looking further up from the reported location, since the parser will stop at the point where things stop making sense and that location is not necessarily where the mistake is (python can’t tell you what you meant)

def censor_list(email_num, to_censor):
    email_num2=email_num.replace('\n',' ' ).split(' ')
    email_num3=[]
    print(email_num2)
    for word in email_num2:
        for piece in to_censor:
            if word == piece:
                email_num3.append(email_num2.strip(word)

                                  else:
                                  email_num3.append(word)
                                  print(email_num3)

                                  censor_list(email_two,["she", "personality matrix",
                                                         "sense of self", "self-preservation",
                                                         "learning algorithm", "her", "herself"])

shouldn’t else be indented like if?

I’m not claiming that’s any kind of valid. But you might consider why that indentation script decides to suddenly add 10-15 spaces of indentation, and why that particular amount, what does that line up with?

I really have no idea.
but if that were the problem wouldn’t I be getting an Indentation error I got a different error:

File "script.py", line 22
    else:
       ^
SyntaxError: invalid syntax

When python reads your code, it matches your text to patterns that it knows.

When you write certain things you are narrowing down what can follow.

For example, if you write:

def

Then the next thing must be a valid name, there’s nothing else valid. If you were to put a reserved keyword there instead, then the parser wouldn’t be able to make sense of it because of the context where you wrote it:

def if 
      ^ space not allowed here

So what is the context that you are writing code in where the error is reported?

You’re trying to fit two lego bricks together with the pegs facing each other

so what is the correct syntax in this case? I can’t see the mistake.

Is an empty file valid code?

If so, then you introduced some invalid code. Where? Hold down backspace until it’s valid.

But you should also be able to backtrace from the error. And when you’re editing you should also notice that you’re introducing a problem, telling you where it is because you know what you just wrote.

whats this?

SyntaxError: unexpected EOF while parsing
def censor_list(email_num, to_censor):
    email_num2=email_num.replace('\n',' ' ).split(' ')
    email_num3=[]
    print(email_num2)
    for word in email_num2:
        for piece in to_censor:
            if word == piece:
                email_num3.append(email_num2.strip(word)

it’s on line 21
and also, I saw where the error was I just didn’t understand why it was an error.

It’s the same thing, no different.

Look at what it says. Something is unexpected. Why would something be unexpected? Because it expected something else because of the context of what you’ve written earlier.

EOF is end of file

You’d get an identical error message if this was your whole code file:

def

Or, it might complain about end of line, depending on whether you terminated that line with \n in your file. So why would EOL or EOF be unexpected? Because after def you have to put a name. Anything else would be unexpected.

The parser doesn’t tell you what your mistake is, it tells you about the state where it was forced to stop, you then have to check the lego bricks that you placed there to make sure they’re well-formed. This is made fairly trivial if you use backspace to remove things, and when it goes away you can undo the backspacing to consider what the last thing was, which is the mistake.

If so then you didn’t see the nature of it, because it’s quite obvious. But where/what is it? You’ll have to get a bit methodical to find that.

When you’re writing code you’re inserting patterns which fit together with other patterns in certain ways. That’s something you need to be in pretty good control of.

But you have the massive advantage of being able to edit the code (backspace being particularly useful here) and seeing if the code then parses.

Another similar situation.

[
def f():
    print('hello world')
]

Perfectly well formed function. But it isn’t allowed there, that’s a list literal, what’s allowed inside a list literal? Expressions. A def-statement is not an expression, it is a statement.

So is the problem with the function? Or the list? Neither, really, but they don’t fit that way, and that’s for the programmer to know and adjust.

since I’m now rewriting the code and I managed to bypass that problem can you tell me what it was cause I’m curious.:wink:

Nothing obscure. If you pick it apart you’ll find it.

You know how the different things there are supposed to look don’t you? for-loops. if-statements. functions. function calls. operators. each of them is a simple thing that fits together with the others in simple ways.

If you look at just two things and how they are fit together, would you be able to find it? I’m completely unconvinced that you wouldn’t.
And, since you have backspace you can eliminate things until there’s only the problem remaining.

When you do find it you’ll think it’s stupid. Yeah, it is, but the part where you figure out how to find it isn’t.

It’s got nothing to do with the problem itself, and everything to do with learning to narrow things down.

it’s missing a )
is that it?

How would you test whether that’s it?

I’ll delete up to there and check for a syntax error