简体   繁体   中英

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.

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 .

tokens = FirstStrike(url)

您必须在调用analytics(tokens)之前将FirstStrike返回值分配给tokens变量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM