Syntax error-python

I got a weird syntax error from typing this

def censor_list(email_num, to_censor):
  for word in to_censor:
    for piece in email_num:
      if word==piece:
        email_num2=email_num.replace(piece,' x ')
  print(email_num2)

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

this is the syntax error:
UnboundLocalError: local variable 'email_num2' referenced before assignment

Hi @temimam,

It is actually an UnboundLocalError, not a SyntaxError. You have this statement:

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

It attempts to pass email_two to the censor_list function from the global scope, but email_two has not been defined in that scope.

so what do I do now?

Yes, you are correct, @ionatan.

The user must have defined that email_two variable in global code that has not been included in the post.

The email_num2 variable is referenced in the function whether or not it has been defined. The conditional block makes it possible for that variable to remain undefined.

email_num2 has been defined in the function censor_list
email_two is imported from another file .txt earlier in the code
@appylpye I’m not sure I understood what that meant
can’t you help me?:cry:

You have this if block:

      if word==piece:
        email_num2=email_num.replace(piece,' x ')

Unless the condition becomes True, the assignment to email_num2 does not execute. The print statement that is outside the for loop and the if block attempts to access that variable regardless of whether it got defined or not.

Think about the logic of the entire function. If some text gets replaced, are you saving that change as you consider the next potential replacement?

maybe I can append every word in the email in to a different variable outside of the if statement something like this

def censor_list(email_num, to_censor):
  email_num2=''
  for word in to_censor:
    for piece in email_num:
      if word==piece:
        email_num2.append(email_num.replace(piece,' x '))
  print(email_num2)

That code has no concept of “every word in the email” (that would be a nicer representation though)

You may also want to consider what the values are that you are getting out of the things that you are iterating through.
What is piece?
What is word?
Does comparing them make sense?

word is word in list of words to censor
piece is word in email to censor
comparing them makes sense because I’m trying to censor each word in the email that is the same as a word in the list of words to censor

You say that. But have you looked whether that’s the case?

For those who are reading this, here is a link to the project: Censor Dispenser.

Here, you define email_num2 as a string:

  email_num2=''

Consequently, the append method will not work here:

        email_num2.append(email_num.replace(piece,' x '))

For strings, you would need concatenation if you wish to add something to the end. However, it may be best to rethink the design of the entire function. You need to preserve each change you make as you proceed to the next change.

you mean to say that piece is iterating through the letters of each word in to_censor ?
but I have it iterating through email_num?!

No, but they are of different shape, so they will not be equal. Take a look at them. Look at all the things you’re dealing with, print is your friend. Print is how you measure the final result, but you can just as well make measurements while the program is still running, and if things aren’t doing what you want then you’d definitely want to start measuring what is going on in there so that you can compare it to what you meant and adjust.

I highly recommend using print to check pretty much anything you ever write so that you can see whether what you wrote has the desired effect. Of course, that only works while working with printable data, but you are. – don’t write code blind, create some feedback and make use of it. When things go wrong in real life you see it and adapt, you can do the same thing when you’re writing code.

I did this to see whats going on:

def censor_list(email_num, to_censor):
  for word in to_censor:
    print(word)
    for piece in email_num:
      print(piece)

and it printed every letter in the email in a single line and I can’t figure out why

Sure you can. Where did you get it from?

get what form where?

every letter

where did you get each letter from? did you expect something else? if so, how would you have obtained that, and how is that different from where you got it from?

Have you at all considered what you would get instead of each letter, and where that would come from?
You would have to write code for that, you’re the one controlling all of that, you’d have to put that in place.

What does a loop do?

If you have many of something and loop through it, then you are getting each thing from the many things aren’t you?

So, you have many letters.

Did you expect to have something else, like many words? Is that what your email is, many words? If it’s many words, it’s because you split it up into words, did you do that, did you mean to do that? If not, it won’t happen, right.

So if you have a string and iterate through it, what do you get? Characters. A string is many characters.

1 Like

I thought I would get each word on a separate line

This will not print words.

for word in 'these are some words':
    print(word)
1 Like