2013-05-11 212 views
-1

我有一個函數可以在字符串元組中插入空格,以便所有字符串在len中相等。我也有一個函數,它爲字符串的元組和一些格式化的信息,並將其combinde爲字符串SyntaxError:無效語法(Python 3.2)

#code for equal string length 
def insertSpace(self,content): 
    max = 0 
    for string in content: 
     temp = len(string) 
     if temp > max: 
      max=temp 
    retstring = ("",) 
    for string in content: 
     retstring = retstring + (" "*(max - len(string)+1,) 

    return self.combine(retstring,content,bold=False,newline=False) 


#code for combine 
def combine(self,leftside,rightside,bold=False,newline=False): 

    if bold is True: 
     bold = '<B>' 
     boldend = '</B>' 
    else: 
     bold = '' 
     boldend = '' 

    if newline is True: 
     newlinechar = '<br>' 
    else: 
     newlinechar = '' 
    return tuple((bold +"{0}"+boldend+"{1}"+newlinechar).format(x,y) for x,y in zip(leftside,rightside)) 

和執行這個腳本的結果的一個元組在此

File "mypythonfile.py", line 108 
return self.combine(retstring,content,bold=False,newline=False) 
    ^
SyntaxError: invalid syntax 

我試圖存儲值在一個變量,但沒有改變。這可能很簡單,但我看不到它。

+0

在我的代碼中發現了一個bug retstring =(「」,)'應該是'retstring =()' – Mattias 2013-05-11 12:26:12

+0

你可以編輯你的問題。 – andlrc 2013-05-11 13:58:26

回答

1

你錯過了這條線關閉)

retstring = retstring + ("&nbsp;"*(max - len(string)+1,)) 
                 ^
                 | 

編輯:在代碼:

>>> 'retstring = retstring + ("&nbsp;"*(max - len(string)+1,)'.count("(") 
3 
>>> 'retstring = retstring + ("&nbsp;"*(max - len(string)+1,)'.count(")") 
2 
+1

我正要寫我的答案,但你太快了蟒蛇:) ..所以在你的答案 – 2013-05-11 08:54:37

+0

謝謝我知道這是簡單的。 – Mattias 2013-05-11 10:37:16

0

Ammend

retstring = retstring + ("&nbsp;"*(max - len(string)+1,) 

retstring = retstring + ("&nbsp;"*(max - len(string)+1,)) #note the closing bracket