Results 1 to 6 of 6

Thread: Getting Error "invalid syntax" in Python

  1. #1
    Join Date
    Jul 2006
    Posts
    442

    Getting Error "invalid syntax" in Python

    I am new to the Python programming language. After making a code in Python, when I am trying to run my code I am getting an error. The following is the error that I am getting
    Code:
    >>> while True print 'Hello world'
      File "<stdin>", line 1, in ?
        while True print 'Hello world'
                       ^
    SyntaxError: invalid syntax
    Also many times I get the exceptions. Please tell me some code for handling the exceptions. Hoping that someone will help me soon..!!
    "When they give you ruled paper, write the other way..." J.R.J.

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: Getting Error "invalid syntax" in Python

    Since you are new that's why you think that this is very tedious job. but actually its simple to troubleshoot such errors. In Python, the parser repeats the offending line and displays a little `arrow' pointing at the earliest point in the line where the error was detected. In your code the error is caused by the token preceding the arrow. So now you came to know that the error is detected at the keyword print. The error was generated, since you missed the colon (":") before it.

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: Getting Error "invalid syntax" in Python

    I am trying to explain you what are an exceptions. Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Exceptions are nothing but the errors that are detected during an execution of the program. Exceptions are not unconditionally fatal. The exceptions come in different types, and the type is printed as part of the message. Standard exception names are built-in identifiers (not reserved keywords). Hope that you got the basic concept of the exceptions.!
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Aug 2006
    Posts
    227

    Re: Getting Error "invalid syntax" in Python

    For handling the exceptions, the Handling Exceptions are used. It is possible to write programs that handle selected exceptions. I have provided you with an example which asks the user for input until a valid integer has been entered, but allows the user to interrupt the program. You will understand if you look at the following example :
    Code:
    >>> while True:
         try:
             x = int(raw_input("Please enter a number: "))
             break
         except ValueError:
             print "That was not a valid number. You will have to try again..."
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  5. #5
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Getting Error "invalid syntax" in Python

    While handling the exceptions you always use the try statement. I have explained the working of the try statement, which is as follows :
    1. First, the try clause (the statement(s) between the try and except keywords) is executed.
    2. The except clause is skipped, if there is no exception and execution of the try statement is finished at that instant.
    3. The rest of the clause is skipped, if an exception occurs during execution of the try clause. The except clause is executed, if its type matches the exception named after the except keyword and then execution continues after the try statement.
    4. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Getting Error "invalid syntax" in Python

    Many times using the raise statement proves beneficial to the programmer. The raise statement allows the programmer to force a specified exception to occur. You can check the following example for detail :
    Code:
    >>> raise NameError, 'HiEveryone'
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    NameError: HiEveryone

Similar Threads

  1. Getting "INVALID CPU TYPE OR SPEED" error with ASRock Z68 Extreme3 Gen3
    By Maryland in forum Motherboard Processor & RAM
    Replies: 3
    Last Post: 10-04-2012, 11:53 AM
  2. Getting the Error "Expression Syntax" in C++
    By MarceloQuad in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 06:35 PM
  3. Replies: 4
    Last Post: 13-11-2009, 07:17 PM
  4. Replies: 3
    Last Post: 30-07-2009, 08:40 PM
  5. Replies: 2
    Last Post: 13-06-2009, 03:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,892,464.12973 seconds with 16 queries