Function name not defined

好久不见. 提交于 2019-12-02 18:48:26

问题


I have a pice of code which looks like this

if __name__ == "__main__":
    main()


def main():
    print("hello")

However, when I try to run this code I get the error

NameError: name 'main' is not defined

Have I not defined the name in the first line of the function "def main()"?


回答1:


You should define main before call it

def main():
    print("hello")

if __name__ == "__main__":
    main()



回答2:


Have I not defined the name in the first line of the function "def main()"?

Yes, but Python hasn't executed that definition yet. Put the function definition before the call.



来源:https://stackoverflow.com/questions/23023246/function-name-not-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!