tags:

views:

363

answers:

7

The code is from K. Pollari-Malmi's lecture notes for the course "Introduction to Programming":

def main():
    print "Ohjelma laskee asuntolainan kuukausierat." 
    rivi = raw_input("Anna lainasumma: ") 
    lainasumma = float(rivi) 
    rivi = raw_input("Anna laina-aika vuosina: ") 
    laina_aika = int(rivi) 
    if laina_aika < 1: 
        print "liian lyhyt laina-aika" 
    else: 
        kk_lkm = 12 * laina_aika 
        rivi = raw_input("Anna korkoprosentti: ") 
        korko = float(rivi) 
        lyhennys = lainasumma / kk_lkm 
        paaoma = lainasumma 
        i = 0 
        print " lyhennys korko yhteensa" 
        while i < kk_lkm: 
            i = i + 1 
            korkoera = korko / 1200.0 * paaoma 
            paaoma = paaoma - lyhennys 
            kuukausiera = korkoera + lyhennys 
            print "%2d. %8.2f %8.2f %8.2f" % \         # mistake probably here
                (i, lyhennys, korkoera, kuukausiera) 
main()

I get the syntax error

SyntaxError: unexpected character after line continuation character

How can you solve the error message?

+2  A: 

Try modifying these lines:

        print "%2d. %8.2f %8.2f %8.2f" % \         # mistake probably here
            (i, lyhennys, korkoera, kuukausiera)

to this line:

        print "%2d. %8.2f %8.2f %8.2f" % (i, lyhennys, korkoera, kuukausiera)

Also, note that a line ending with a backslash cannot carry a comment. So your #mistake probably here comment is likely causing the problem.

Randolpho
+5  A: 

You can't have anything, even whitespace, after the line continuation character.

Either delete the whitespace, or wrap the entire line in a pair of parentheses. Python implicitly joins lines between parentheses, curly braces, and square brackets:

print ( "%2d. %8.2f %8.2f %8.2f" % 
        (i, lyhennys, korkoera, kuukausiera) )
Triptych
+2  A: 

Try rewriting this:

print "%2d. %8.2f %8.2f %8.2f" % \         # mistake probably here
            (i, lyhennys, korkoera, kuukausiera)

To this:

print ("%2d. %8.2f %8.2f %8.2f" %
       (i, lyhennys, korkoera, kuukausiera))

\ should work too, but IMO it's less readable.

Bastien Léonard
+2  A: 

Replace:

print "%2d. %8.2f %8.2f %8.2f" % \
    (i, lyhennys, korkoera, kuukausiera)

By:

print "%2d. %8.2f %8.2f %8.2f" % (
    i, lyhennys, korkoera, kuukausiera)

General remark: always use English for identifiers

J.F. Sebastian
To the General who remarked this WHY always use English for identifiers ?
Blauohr
I personally agree about English, but the Python consensus does not: indeed, in Python 3 you're allowed to use non-ASCII alphamerics in identifiers, too, to make your identifiers "proper" non-English. I grumbled about it at the time but Guido was adamant!-)
Alex Martelli
@Blauchr, non-English identifiers limit you to sensibly discussing and sharing your code with a relatively tiny local community, English lets you easily cooperate (include meaningfully asking question and receiving advice) on a worldwide basis. I'm not a native speaker of English, but I can help people who use English identifiers much better than those who use Finnish, Mandarin, Arabic, Japanese, etc, etc -- and that's pretty common!-)
Alex Martelli
+10  A: 

Several answers already gave you the crux of your problem, but I want to make a plug for my favorite way to get logical line continuation in Python, when feasible:

print "%2d. %8.2f %8.2f %8.2f" % (         # no mistake here
            i, lyhennys, korkoera, kuukausiera)

i.e., instead of using extra parentheses as some answers advise, you can take advantage of any parenthesis you already naturally happen to have -- that will tell Python that you need logical line continuation, too;-)

Alex Martelli
+1 for your favorite way !
Blauohr
@Alex: Thank you for your answer! I see that your way is the best one.
Masi
=/ i don't like it. The lone paren on the first line bother me.
Triptych
Careful with this, however. It'll break if the code gets used in a Python 3.0 implementation-- given the new print syntax. Not so much an issue to you now, just something to be conscious of.
JohnMetta
@mettadore: Could you give an example how we use the print in Python 3.1 for the same purpose? -- I am reading the docs at http://docs.python.org/dev/py3k/library/functions.html?highlight=print#print
Masi
The 2to3 code conversion tool that comes with Python 2.6 and Python 3 has no trouble converting Python 2's print stataments into Python 3 print function calls (in this case, it will insert the parentheses that the print function needs and the print statement doesn't).
Alex Martelli
@Triptych, the "lone paren" is definitely better than the sole positive alternative, a lone backslash -- the latter's very fragile and picky in terms of placement, trailing space, etc, etc. The choice is _where_ to place that lone paren, and my preference is to have it at the point where, syntactically, a paren's needed anyway.
Alex Martelli
+1  A: 

In general, I find I don't use line continuation in Python. You can make it cleaner with parentheses and so on.

Sean Cavanagh
A: 

"\" means "this line continue to the next line" and it can't have any character after it. There probably is a space right after.

Two solution :

Ensure there is not spaces after;

Rewrite the statement on one line :

print "%2d. %8.2f %8.2f %8.2f" % (i, lyhennys, korkoera, kuukausiera)

You can even use parenthesis to make it fit on several lines :

print "%2d. %8.2f %8.2f %8.2f" % (
       i, lyhennys, korkoera, kuukausiera)
e-satis