【问题标题】:Getting Error while running program运行程序时出错
【发布时间】:2017-01-12 02:57:57
【问题描述】:
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()

【问题讨论】:

  • 您需要将问题的格式编辑为代码。

标签: python python-2.7 python-3.x


【解决方案1】:

您可能还有其他错误,但肯定没有声明name,所以

if name == 'main'

会给你一个错误。相反,它应该是

if __name__ == '__main__'

【讨论】:

  • 你能尝试一下那个代码并告诉我那个错误是什么
  • @chichu 你是谜语人吗?我是否应该将这一堆纯文本作为 Python 代码运行,以便找出您遇到的错误?
猜你喜欢
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
相关资源
最近更新 更多