LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2014, 07:15 PM   #1
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Rep: Reputation: Disabled
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!")
 
Old 07-08-2014, 07:44 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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!")
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ldap_bind: Invalid DN syntax (34) additional info: invalid DN eswar425 Linux - Newbie 1 01-01-2014 10:08 AM
Invalid Syntax Error on Python Function antonia_alessandra Linux - Newbie 3 03-31-2013 02:49 AM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
[Python] Invalid Syntax - If statement LinuxCrayon Programming 4 12-07-2007 09:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration