syntaxerror: "unexpected character after line continuation character in python" math
Asked Answered
A

7

22

I am having problems with this Python program I am creating to do maths, working out and so solutions but I'm getting the syntaxerror: "unexpected character after line continuation character in python"

this is my code

print("Length between sides: "+str((length*length)*2.6)+" \ 1.5 = "+str(((length*length)*2.6)\1.5)+" Units")

My problem is with \1.5 I have tried \1.5 but it doesn't work

Using python 2.7.2

Ampulla answered 17/10, 2011 at 9:40 Comment(0)
D
26

The division operator is /, not \

Derril answered 17/10, 2011 at 9:47 Comment(1)
I switched to using *0.666666667 then I saw this...I might just stick to what I have written now.Ampulla
S
19

The backslash \ is the line continuation character the error message is talking about, and after it, only newline characters/whitespace are allowed (before the next non-whitespace continues the "interrupted" line.

print "This is a very long string that doesn't fit" + \
      "on a single line"

Outside of a string, a backslash can only appear in this way. For division, you want a slash: /.

If you want to write a verbatim backslash in a string, escape it by doubling it: "\\"

In your code, you're using it twice:

 print("Length between sides: " + str((length*length)*2.6) +
       " \ 1.5 = " +                   # inside a string; treated as literal
       str(((length*length)*2.6)\1.5)+ # outside a string, treated as line cont
                                       # character, but no newline follows -> Fail
       " Units")
Semele answered 17/10, 2011 at 9:46 Comment(0)
E
11

You must press enter after continuation character

Note: Space after continuation character leads to error

cost = {"apples": [3.5, 2.4, 2.3], "bananas": [1.2, 1.8]}

0.9 * average(cost["apples"]) + \ """enter here"""
0.1 * average(cost["bananas"])
Electronarcosis answered 29/1, 2018 at 16:1 Comment(1)
Wow - this little nugget of information - spent a lot of time trying to figure out why the code wasn't working; didn't know about "pressing enter immediately after the line continuation character" thanks a ton!Allpurpose
A
2

The division operator is / rather than \.

Also, the backslash has a special meaning inside a Python string. Either escape it with another backslash:

"\\ 1.5 = "`

or use a raw string

r" \ 1.5 = "
Aggrieved answered 17/10, 2011 at 9:42 Comment(0)
P
0

Well, what do you try to do? If you want to use division, use "/" not "\". If it is something else, explain it in a bit more detail, please.

Pigeontoed answered 17/10, 2011 at 9:47 Comment(0)
R
0

As the others already mentioned: the division operator is / rather than **. If you wanna print the ** character within a string you have to escape it:

print("foo \\")
# will print: foo \

I think to print the string you wanted I think you gonna need this code:

print("Length between sides: " + str((length*length)*2.6) + " \\ 1.5 = " + str(((length*length)*2.6)/1.5) + " Units")

And this one is a more readable version of the above (using the format method):

message = "Length between sides: {0} \\ 1.5 = {1} Units"
val1 = (length * length) * 2.6
val2 = ((length * length) * 2.6) / 1.5
print(message.format(val1, val2))
Radar answered 17/10, 2011 at 9:59 Comment(0)
C
0

This is not related to the question; just for future purpose. In my case, I got this error message when using regex. Here is my code and the correction

text = "Hey I'm Kelly, how're you and how's it going?"
import re

When I got error:

x=re.search(r'('\w+)|(\w+'\w+)', text)

The correct code:

x=re.search(r"('\w+)|(\w+'\w+)", text)

I'm meant to use double quotes after the r instead of single quotes.

Cutanddried answered 9/12, 2022 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.