语法错误:“Python 中的行继续字符后出现意外字符”数学
我在创建这个Python程序来做数学、计算等解决方案时遇到问题,但我收到语法错误:“python中的行继续字符后出现意外的字符”
这是我的代码:
print("Length between sides: "+str((length*length)*2.6)+" \ 1.5 = "+str(((length*length)*2.6)\1.5)+" Units")
我的问题是\1.5
。我尝试过 \\1.5
但它也不起作用。
使用Python 2.7.2
I am having problems with this Python program I am creating to do maths, working out and so solutions but I'm getting the syntax error: "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 either.
Using python 2.7.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除法运算符是
/
,而不是\
The division operator is
/
, not\
反斜杠
\
是错误消息所讨论的行继续字符,在它之后,只允许换行符/空格(在下一个非空格继续“中断”行之前)。字符串,反斜杠只能以这种方式出现。对于除法,您需要斜杠:
/
。如果要在字符串中逐字写入反斜杠,请通过将其加倍来转义:
"\\"
在您的代码中,您使用了它两次:
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.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:
您必须在连续字符后按 Enter
注意:连续字符后的空格会导致错误
You must press enter after continuation character
Note: Space after continuation character leads to error
除法运算符是
/
而不是\
。此外,反斜杠在 Python 字符串中具有特殊含义。使用另一个反斜杠对其进行转义:
或使用原始字符串
The division operator is
/
rather than\
.Also, the backslash has a special meaning inside a Python string. Either escape it with another backslash:
or use a raw string
好吧,你想做什么?如果要使用除法,请使用“/”而不是“\”。
如果是其他情况,请更详细地解释一下。
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.
正如其他人已经提到的:除法运算符是 / 而不是 **。
如果你想打印字符串中的 ** 字符,你必须转义它:
我想打印你想要的字符串我想你需要这段代码:
这是一个更具可读性的版本上面(使用格式化方法):
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:
I think to print the string you wanted I think you gonna need this code:
And this one is a more readable version of the above (using the format method):
这与问题无关;只是为了未来的目的。就我而言,我在使用正则表达式时收到此错误消息。 更正
这是我的代码和当我遇到错误时的
:正确的代码:
我打算在 r 之后使用双引号而不是单引号。
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
When I got error:
The correct code:
I'm meant to use double quotes after the r instead of single quotes.