syntaxerror:“python中的换行符后出现意外字符”math

2024-05-16 01:01:31 发布

您现在位置: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但它不起作用

使用Python2.7.2


Tags: 程序密码数学between解决方案字符lengthunits
3条回答

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

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

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

此外,反斜杠在Python字符串中有特殊的含义。用另一个反斜杠转义:

"\\ 1.5 = "`

或使用原始字符串

r" \ 1.5 = "

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

相关问题 更多 >