Computer Science A day Do Now Due TODAY 2:30 AM Manzoor Syntax Errors

Q1.What are syntax errors in Python language?

Q2. How can I detect syntax errors?


Notes:
1) Syntax errors are the most basic type of error
They 
arise when the Python parser is unable to 
understand a line of code. Syntax errors 
are almost always fatal, i.e. there is almost 
never a way to successfully execute 
a piece of code containing syntax errors.
  
2) From:
http://www.openbookproject.net/thinkcs/python/english2e/app_a.html

Syntax errors

Syntax errors are usually easy to fix once you figure out what they are. Unfortunately, the error messages are often not helpful. The most common messages are SyntaxError: invalid syntax and SyntaxError: invalid token, neither of which is very informative.
On the other hand, the message does tell you where in the program the problem occurred. Actually, it tells you where Python noticed a problem, which is not necessarily where the error is. Sometimes the error is prior to the location of the error message, often on the preceding line.
If you are building the program incrementally, you should have a good idea about where the error is. It will be in the last line you added.
If you are copying code from a book, start by comparing your code to the book’s code very carefully. Check every character. At the same time, remember that the book might be wrong, so if you see something that looks like a syntax error, it might be.
Here are some ways to avoid the most common syntax errors:
  1. Make sure you are not using a Python keyword for a variable name.
  2. Check that you have a colon at the end of the header of every compound statement, including for, while, if, and def statements.
  3. Check that indentation is consistent. You may indent with either spaces or tabs but it’s best not to mix them. Each level should be nested the same amount.
  4. Make sure that any strings in the code have matching quotation marks.
  5. If you have multiline strings with triple quotes (single or double), make sure you have terminated the string properly. An unterminated string may cause an invalid token error at the end of your program, or it may treat the following part of the program as a string until it comes to the next string. In the second case, it might not produce an error message at all!
  6. An unclosed bracket – (, {, or [ – makes Python continue with the next line as part of the current statement. Generally, an error occurs almost immediately in the next line.
  7. Check for the classic = instead of == inside a conditional.
If nothing works, move on to the next section...

I can’t get my program to run no matter what I do.

If the compiler says there is an error and you don’t see it, that might be because you and the compiler are not looking at the same code. Check your programming environment to make sure that the program you are editing is the one Python is trying to run. If you are not sure, try putting an obvious and deliberate syntax error at the beginning of the program. Now run (or import) it again. If the compiler doesn’t find the new error, there is probably something wrong with the way your environment is set up.
If this happens, one approach is to start again with a new program like Hello, World!, and make sure you can get a known program to run. Then gradually add the pieces of the new program to the working one.

Comments

Popular posts from this blog

Computer Science Manzoor Do Now Due 250 PM

Computer Science Manzoor Do Now and Class due 2:50 PM

Bday Math Pre-Calc M. Mui Pecentage increase/decrease do now Class: Jeopardy due 2:30 Pm