+ 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💔💔🙏

27th Aug 2020, 6:44 AM
Nickson Osiemo
Nickson Osiemo - avatar
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.
27th Aug 2020, 7:02 AM
Théophile
Théophile - avatar
+ 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).
27th Aug 2020, 6:51 AM
Théophile
Théophile - avatar
0
ThĂ©ophile I don't understand why the __name__ Is not equal to __main__ inside my code...💔
27th Aug 2020, 6:58 AM
Nickson Osiemo
Nickson Osiemo - avatar
0
Thanks
27th Aug 2020, 7:06 AM
Nickson Osiemo
Nickson Osiemo - avatar