[Tutor] Help

Steven D'Aprano steve at pearwood.info
Thu Mar 14 03:38:24 CET 2013


On 14/03/13 02:12, Joshua Wilkerson wrote:
> Can you help me with something? This code (it also draws from the text_game file) says it has a syntax error, but I can't seem to find what it is, I think the code is having a fit but I'm not sure. I'm appreciative to all hep.


The most valuable help we can give you is to teach you to help yourself, and to learn what not to do. Here are some Do Nots:

- Do not expect us to read through hundreds of lines of code looking for the error.

- Do not expect us to save your files and then run them. We have no idea what they will do, you might be trying to trick us into running harmful code. Unless we read through all your code and study it carefully, we can't know if it is safe.

- Even if we trust you AND trust your code, we're volunteers, not servants. We're only going to install your code and run it if you pay us, or if your problem seems so interesting that we want to solve the problem no matter how much work it takes. Unfortunately, "a syntax error" does not sound interesting.

- Don't assume that Python is "having a fit". Trust me, hundreds of thousands or millions of people have used Python for 20+ years now. No software is perfect and bug free, but trust me, the chances that you have discovered a bug that nobody before you has ever seen is remote. 99.99% of the bugs you experience as a programmer will be bugs in *your* code, not the language. (But don't worry, you'll soon learn to fix those bugs so quickly you won't even remember them.)


And here are some Dos:


- The most valuable thing you can do right now is learn how to read and understand the error messages that Python gives you. There is a lot of information buried in them. And usually not buried very deeply, often all you need to do is read it and it will tell you what went wrong. (At least once you get experiences enough to know how to interpret the error.) Compared to some other languages, Python's error messages are a paragon of clarity and simplicity.

See below for more on this one.

- If you have trouble with an error that you can't solve yourself, make it easy for us to help you:

   * The absolute LEAST you need to do is copy and paste the entire
     traceback, starting with the line "Traceback (most recent call last)"
     all the way to the end, including the error message. SyntaxErrors may
     not have a Traceback line, but you should still copy and paste the
     entire message.

   * Better still, if you can, try to SIMPLIFY the problem to the smallest
     amount of code that displays the same error. Nine times out of ten, by
     going through this process of simplifying the code, *you* will discover
     what the error was, and solve the problem yourself. The tenth time, you
     will then have a nice, simple piece of code that you can show us,
     instead of hundreds and hundreds of lines that we won't read.

     See this website for more detail:

     http://sscce.org/

     Although it is written for Java programmers, the lessons it gives apply
     to any language.

   * Remember to mention what version of Python you are using, and what
     operating system. (Windows, Linux, Mac, something else?) If you are
     using a less common Python compiler, like IronPython or Jython, say so.
     If you are running your code via an IDE like IDLE or IPython, say so.



Now for this specific error. You are getting a SyntaxError. Unfortunately, syntax errors sometimes give the least informative error messages in Python. But fortunately you can still work out what is wrong:


py> for x = range(1, 2):
   File "<stdin>", line 1
     for x = range(1, 2):
           ^
SyntaxError: invalid syntax


Notice the small caret ^ on a line on its own? In your email, it may not line up correctly, but if you read the error in context, in the Python compiler or IDE where it occurs, it will line up with the first thing that Python found that was broken syntax. In this case, you simply cannot use assignment, "x = something", inside the for line. The right syntax is "for x in range(1, 20)".

Here's another example:


py> x = 23*)1+5)
   File "<stdin>", line 1
     x = 23*)1+5)
            ^
SyntaxError: invalid syntax


Here the caret should line up under the left-most parenthesis (round bracket). The solution is to use the open-bracket, not close-bracket.


A common error is to use too few closing brackets.

py> x = 23*(1 + (15 - 7)
... y = x + 1
   File "<stdin>", line 2
     y = x + 1
     ^
SyntaxError: invalid syntax


See what happens here? The error, the missing bracket, is on the *previous* line. That's because Python cannot tell that it is truly missing until it gets to the next line. So if you have a Syntax Error on a line that looks right, or that is a comment, *work backwards*, line by line, until you find something that is missing a closing bracket.



-- 
Steven


More information about the Tutor mailing list