[Tutor] Program Problems (original) (raw)

Magnus Lyckå magnus at thinkware.se
Fri Jul 16 14:08:36 CEST 2004


At 02:40 2004-07-16 +0000, mandy bowen wrote:

If you see a problem, can you tell me?

http://rafb.net/paste/results/88yf5V76.html I cannot figure out the errors.

The syntax error on the print statement at

     if option == 1:
         Account.deposit(input("Deposit amount:$")
         print "TRANSACTION COMPLETED"

is because you lack a ")" on the previous line. A logical line in Python extends through several physical lines in Python while you are inside {}, [] or (), so that you can write things like:

def func(parameter1, parameter2, parameter3, parameter4, parameter5, parameter6, parameter7, parameter8, parameter9, parameter10, parameter11, parameter12):

This means that your code is interpreted as

     if option == 1:
         Account.deposit(input("Deposit amount:$") print "TRANSACTION 

COMPLETED" [...rest of program]

Of course, you can't have a print statement inside the method call to Account.deposit().

It's very common that errors found by interpreters and compilers are actually caused by something on the line above. Python can't start complaining about the line where you forgot the ), since it's correct to write your code like that.

A "smarter" compiler could have see that there aren't matching ) in the rest of the file, and given a proper diagnostic message for that, but the current approach of going through the code linearly and stopping at the first encountered error, has some advantages from a debugging point of view, and it's probably much easier to maintain the code and to keep the performance up with this simple approach.

Of course, the error message could hint about the possibility of this problem, but this is such a common thing that most programmers (in all languages I've coded in) will check for a thing like that on reflex when they see an error message like that. The fact that it complained about something else when you removed the print statements must in hindsight be seen as a hint that your problem was actually somewhat earlier in the code, but I've certainly been confused about that kind of error myself in some long forgotten past. (Probably while coding Pascal.)

Or this list.

Are you saying that you can't figure out the list or that the list can't figure out the errors? ;)

-- Magnus Lycka (It's really Lyckå), magnus at thinkware.se Thinkware AB, Sweden, www.thinkware.se I code Python ~ The Agile Programming Language



More information about the Tutor mailing list