LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python Newbie: SyntaxError: invalid syntax (https://www.linuxquestions.org/questions/programming-9/python-newbie-syntaxerror-invalid-syntax-4175510528/)

gdizzle 07-08-2014 07:15 PM

Python Newbie: SyntaxError: invalid syntax
 
Hi this has a problem with the last "else:" in the python code and I am not sure why? I am a massive newbie to scripting so any help would be great?

python pythontest.py
File "pythontest.py", line 24
else:
^
SyntaxError: invalid syntax

Thanks

Code:

#!/usr/bin/python

import sys
import re

#assigns a variable
len1 = len(sys.argv)


if len1 <2:
        print ("Need at least 1 ARG: Supplied", len1, "ARGS", sys.argv[0])
        exit(200)
elif len1 >2:
        print ("You have supplied too many ARGS", len1, )
        exit("TOO MANY ARGS")
else:
        print ("You have supplied", len1, " ARGS")
        #pass keeps script going without doing anything
        pass
        exit()
if re.search('^\d*$', sys.argv[1]):
        print("MATCH")
        else:
        print ("NO MATCH")
        exit (200)
print ("You have passed!")


dugan 07-08-2014 07:44 PM

That else is indented wrong.

Code:

if re.search('^\d*$', sys.argv[1]):
        print("MATCH")
else:
        print ("NO MATCH")
        exit (200)
print ("You have passed!")



All times are GMT -5. The time now is 08:16 AM.