2014-09-10 68 views
-1

我們必須使用由13個字符組成的字符串創建一個圖騰柱。每個頭部必須具有不同的特徵。我的一些特色功能如下。但是,當我運行我的代碼時,它給了我上面的語法錯誤。SyntaxError:掃描字符串文字時的EOL:TOTEM

import random 

def hair_spiky(): 
     return"/\/\/\/\/\/\/" 

def hair_middlepart(): 
     return"\\\\\\ //////" 

def eye_crossed(): 
     a1= r" ____ ____" 
     a2= r"/ \/ \" 
     a3= r"| O| |O |" 
     a4= r"\____/ \____/" 
     return a1 +"\n" + a2 + "\n" + a3 + "\n" a4 
    def eye_left(): 
     a1=r" ____ ____" 
     a2=r"/ \/ \" 
     a3=r"|O | |O |" 
     a4=r"\____/ \____/" 
+0

你不能用'\\'結束一個字符串(甚至是原始字符串)。 – 2014-09-10 20:24:03

回答

1

你不能在raw string literal使用\作爲最後一個字符:

r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).

不要使用原始字符串存在;要加倍反斜槓代替:

a2= "/ \\/ \\" 

或通過使用三引號使用原始多行字符串:

def eye_crossed(): 
    return r""" 
____ ____ 
/ \/ \ 
| O| |O | 
\____/ \____/"""[1:] 

def eye_left(): 
    return r""" 
____ ____ 
/ \/ \ 
|O | |O | 
\____/ \____/"""[1:] 

切片被用於去除初始換行符其是字符串的一部分。

0

這是原始字符串的奇夸克,並通過語法高亮差表示,看到lexical analysis

Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes)

要修復它使用普通字符串和/或級聯:

"/ \\/ \\" 
r"/ \/ " + '\\'