1

My question might be simple but I really cannot figure out where I went wrong. I would like to pass one variable from a function to another function. I use return therefore but I'm always getting an error message, that my variable is not defined.

My code is:

url = "http://www.419scam.org/emails/2004-01/30/001378.7.htm"

def FirstStrike(url):
    ...
    return tokens

def analyze(tokens):
    ...

if __name__ == "__main__":
    FirstStrike(url)
    analyze(tokens)

If I run this I got an error message: NameError: name 'tokens' is not defined.

2
  • 1
    A return statement returns an object, not a name or a "variable". Commented Jun 9, 2012 at 13:38
  • 1
    Rather than making people read your whole source file, a backtrace would have made answering much easier. So would minimizing the code to the smallest bit that fails in a way you don't understand. I have chopped out all but the relevant bits of your code sample, if you did the same, your problem might have been more obvious to you. Commented Jun 9, 2012 at 13:44

2 Answers 2

9

When you run the code, you haven't assigned the result of FirstStrike to a variable:

if __name__ == "__main__":
    tokens = FirstStrike(url)
    analyze(tokens)

This is necessary because otherwise tokens is not defined when you call analyze.

Sign up to request clarification or add additional context in comments.

Comments

4
tokens = FirstStrike(url)

You must assign FirstStrike return value to tokens variable before calling analyze(tokens)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.