+ 2
Help me learn to use the main function in python???
I am trying to come up with a game but when I use the main function and run the entire code .. I get an error that main is not defined ... Am using pygame in pycharm Please help međđđ
5 Answers
+ 5
If you have a script called 'main.py', and tou start this script with the command 'py main.py', then inside this 'main.py' script, __name__ is equal to __main__.
Now, what if from your 'main.py' script you import a function from another file 'functions.py'.
Then inside 'functions.py', __name__ will not be equal to __main__ since it is not the main script launched with python.
+ 4
Defining a main function in Python is totally useless.
Using 'if __name__ == "__main__":', on the contrary, makes sense.
The value of __name__ is equal to '__main__' only if it is the main script of your code (the script directly launched by python).
0
Théophile
I don't understand why the __name__ Is not equal to __main__ inside my code...đ
0
Thanks