0

I am working on leetcode but actually never wrote a file locally before.

class Solution(object):
    def singleNumber(self, nums):

        for i in range(0,len(nums),2):
            if (i != len(nums) - 1) and (nums[i] != nums[i+1]):
                print (nums[i])
            elif i == len(nums) - 1:
                print (nums[i])

    def main():
        nums=[1,1,2,2,3]
        s=Solution()
        s.singleNumber(nums)
        print('done')

I am running this script but not seeing any output and don't understand what I'm doing wrong.

2
  • With local python programs, the main code is always executed outside of a class. So you need to move your code inside "def main()" outside of all functions and classes, with no indentation. Then remove the "def main()", as it's not the main method you're thinking it is. Commented Oct 10, 2016 at 23:14
  • Why did you make main a method of Solution? Commented Oct 10, 2016 at 23:14

2 Answers 2

2

Unlike many other programming languages, such as Java, Python does not require the main method to be inside a class. Even more, Python does not need to have a main method defined: it runs the whole file as an application. In your original post, Python performs this actions:

  • Define the method singleNumber as the code it holds.
  • Define the method main as the code it holds.
  • Save this two methods inside the class Solution.
  • No more lines to run, therefore ends application.

To make the application correct, you must write it as follows:

class Solution(object):
    def singleNumber(self, nums):
        for i in range(0,len(nums),2):
            if (i != len(nums) - 1) and (nums[i] != nums[i+1]):
                print (nums[i])
            elif i == len(nums) - 1:
                print (nums[i])

if __name__=='__main__':
    nums=[1,1,2,2,3]
    s=Solution()
    s.singleNumber(nums)
    print('done')

You may wonder why the line if __name__=='__main__':; every file contains the variable __name__ implicitly defined and its value depends if you're running the file directly or it is imported. On the first case, the assignment __name__='__main__' is done, on the second case the __name__ variable is assigned the name of the file itself; this gives you an idea whether this file is the main or not.

You may also discard the Solution class and promote the singleNumber method to a module method.

Sign up to request clarification or add additional context in comments.

Comments

2

Move your main function outside of the class, and then specifically execute it:

class Solution(object):
    def singleNumber(self, nums):

        for i in range(0,len(nums),2):
            if (i != len(nums) - 1) and (nums[i] != nums[i+1]):
                print (nums[i])
            elif i == len(nums) - 1:
                print (nums[i])

def main():
    nums=[1,1,2,2,3]
    s=Solution()
    s.singleNumber(nums)
    print('done')

main()

Output:

3
done

Another possibility is to make it a separate function at all: drop the def main line, un-indent the four lines of that code, and run it from the top level.

1 Comment

I'm not sure dropping def main is objectively better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.