【问题标题】:Cannot see the output for the simple "hello world" python code in terminal在终端中看不到简单的“hello world”python 代码的输出
【发布时间】:2016-05-30 07:05:16
【问题描述】:

我不明白为什么我在运行以下代码(Python 2.7)时在终端上看不到输出:

    #!/usr/bin/python

    import sys

    def main():

      if len(sys.argv) >=2:
        name = sys.argv[1]
      else:
        name = "Heisenberg"
      print "Hello", name

    if "__name__" == "__main__":
      main()

【问题讨论】:

  • 首先,如果你没有在子句之前声明/使用 name,那么 name 在 if 和 else 分支中都是本地的...

标签: python macos


【解决方案1】:

__name__应该没有"

if __name__ == "__main__": 

"__name__" 是一个字符串,所以它永远不会等于"__main__"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2021-02-07
    • 2010-10-07
    • 2019-05-08
    • 2011-10-17
    • 2022-01-25
    相关资源
    最近更新 更多