01000001
01000001

Reputation: 97

Variables scope in python

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.

Upvotes: 1

Views: 135

Answers (2)

shiva
shiva

Reputation: 2770

tokens = FirstStrike(url)

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

Upvotes: 4

Simeon Visser
Simeon Visser

Reputation: 122336

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.

Upvotes: 9

Related Questions