语法错误:“Python 中的行继续字符后出现意外字符”数学

发布于 2024-12-10 07:44:11 字数 305 浏览 0 评论 0原文

我在创建这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

∞梦里开花 2024-12-17 07:44:11

除法运算符是 /,而不是 \

The division operator is /, not \

相权↑美人 2024-12-17 07:44:11

反斜杠 \ 是错误消息所讨论的行继续字符,在它之后,只允许换行符/空格(在下一个非空格继续“中断”行之前)

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

。字符串,反斜杠只能以这种方式出现。对于除法,您需要斜杠:/

如果要在字符串中逐字写入反斜杠,请通过将其加倍来转义: "\\"

在您的代码中,您使用了它两次:

 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")

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")
别挽留 2024-12-17 07:44:11

您必须在连续字符后按 Enter

注意:连续字符后的空格会导致错误

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"])

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"])
谈情不如逗狗 2024-12-17 07:44:11

除法运算符是 / 而不是 \

此外,反斜杠在 Python 字符串中具有特殊含义。使用另一个反斜杠对其进行转义:

"\\ 1.5 = "`

或使用原始字符串

r" \ 1.5 = "

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 = "
像你 2024-12-17 07:44:11

好吧,你想做什么?如果要使用除法,请使用“/”而不是“\”。
如果是其他情况,请更详细地解释一下。

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.

天涯离梦残月幽梦 2024-12-17 07:44:11

正如其他人已经提到的:除法运算符是 / 而不是 **
如果你想打印字符串中的 ** 字符,你必须转义它:

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

我想打印你想要的字符串我想你需要这段代码:

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

这是一个更具可读性的版本上面(使用格式化方法):

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))

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))
等你爱我 2024-12-17 07:44:11

这与问题无关;只是为了未来的目的。就我而言,我在使用正则表达式时收到此错误消息。 更正

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

这是我的代码和当我遇到错误时的

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

:正确的代码:

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

我打算在 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文