Jump to content

syntax error when trying to run main() in python 3.7

PorterG2003
def mergeSort(lst):
    lst = [[i] for i in lst]
    while len(lst) > 1:
        for i in lst:
            c = 0
            if lst.index(i) == len(lst)-1:
                k = lst[lst.index(i)-1]
            else:
                k = lst[lst.index(i)+1]
            while len(k) > 0:
                if c == len(i):
                    i.append(k.pop(0))
                elif i[c] > k[0]:
                    i.insert(c, k.pop(0))
                    c += 1
                else:
                    c += 1

            lst.remove(k)
    return lst

def main():
    print()
    print(mergeSort([random.randint(-1000,1000) for i in range(10000)])

main()

Anyway to fix this?

Capture.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

At the very bottom, add:

if __name__ == '__main__':

    sys.exit(main())

instead of main()

 

And as the very first line, add 

import sys

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, 79wjd said:

At the very bottom, add:


if __name__ == '__main__':

    sys.exit(main())

instead of main()

 

And as the very first line, add 


import sys

that gave me an error for the colon after the if statement then one for syntax of sys.exit(main())

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, PorterG2003 said:

that gave me an error for the colon after the if statement then one for syntax of sys.exit(main())

What's the error? Also try a new file with something simple like:

 

import sys

 

def main():

    print("hello")

 

if __name__ == '__main__':

    sys.exit(main ())

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

It's a syntax error so adding that "if" won't solve it. There is one ) missing:

 

print(mergeSort([random.randint(-1000,1000) for i in range(10000)])
Link to comment
Share on other sites

Link to post
Share on other sites

On 8/9/2018 at 7:04 AM, 79wjd said:

What's the error? Also try a new file with something simple like:

 

import sys

 

def main():

    print("hello")

 

if __name__ == '__main__':

    sys.exit(main ())

While good practice, it's not the problem.  That does nothing to make it run, it only makes it NOT execute if the module is imported as opposed to being the initial entry point....hence good practice.

@riklaunim got it with the missing closing parenthesis.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×