Reputation: 6193
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()"?
Upvotes: 0
Views: 1726
Reputation: 156
You should define main before call it
def main():
print("hello")
if __name__ == "__main__":
main()
Upvotes: 5
Reputation: 184091
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.
Upvotes: 5