2011-10-17 40 views
1

我遇到了這個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

回答

8

除法運算符是/,不\

+5

現在我覺得相當愚蠢,但謝謝你。 – Arcticfoxx

+0

我轉向使用* 0.666666667,然後我看到了......我可能只是堅持我現在寫的東西。 – Arcticfoxx

2

除法運算符是/而不是\

另外,反斜槓在Python字符串中有特殊的含義。或者與另一個反斜槓轉義:

"\\ 1.5 = "` 

,或者使用一個原始字符串

r" \ 1.5 = " 
6

反斜槓\是錯誤消息在談論續行字符,並經過它,只有換行符/空白允許(下一個非空白延續了「打斷」前行。

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

外字符串,反斜槓只能AP梨這樣。對於劃分,你需要一個斜線:/

如果你想要寫一個字符串逐字反斜槓,增加一倍它逃避它:"\\"

在代碼中,你使用它兩次:

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

嘛,做什麼你試着做?如果你想使用除法,使用「/」而不是「\」。 如果是別的,請詳細解釋一下。

0

前面已經提到過別人:除法運算符是/而不是* *。 如果你想在字符串中打印* *字符,你必須逃避它:

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

您必須在連續之後按回車性格

注:連字符後的空間導致錯誤

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"]) 
相關問題