简体   繁体   English

运行程序时出错

[英]Getting Error while running program

try:
    import sys
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    import sys
    from urllib2 import urlopen

def fetch_words(url):
    html = urlopen(url)
    print(html.read())
    story_words = []
    for line in html:
        line_words = line.decode('utf-8').split()
    for word in line_words:
        story_words.append(word)
    return story_words


def print_items(items):
    for item in items:
        print(item)


def main():
    url = sys.argv[1]
    words = fetch_words(url)
    print_items(words)

if name == '__main__'
    main()

You might have other errors, but surely, name is not declared, so您可能还有其他错误,但肯定没有声明name ,所以

if name == 'main'

will raise you an error.会给你一个错误。 Instead, it should be相反,它应该是

if __name__ == '__main__'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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