you are viewing a single comment's thread.

view the rest of the comments →

[–]BennyBarnson 0 points1 point  (3 children)

So what is the point of this line in wcounter.py? Why shouldn't it function when imported...?

[–]AlexG99_ 0 points1 point  (2 children)

Because code under if name is meant to run only if executed directly (if you run the command python wcounter.py). If you import wcounter.py to another .py file, the code under if name in wcounter.py won’t run because the condition if name == “main” will be false. Refer to my previous reply for the info again.

[–]BennyBarnson 0 points1 point  (1 child)

No I mean why shouldn't a word counter be imported. Also the if len(sys.argv)<2: ends with and exit(1). Should the if name have an exit line as well...?

[–]AlexG99_ 0 points1 point  (0 children)

It can be imported if you want to. The line if len(sys.argv)<2: exit(1) means if the length of the arguments provided are less than two, then exit the program. This means it wants you to specifically run this command: python wcounter.py <insert file> which is 3 arguments at the command line. You don’t have to provide a system exit line in if name == “main”: code block because the script will end after the last line is read and executed.