SyntaxError:语法无效(Python 3.2)

时间:2013-05-11 08:49:23

标签: python python-3.x syntax-error

我有一个函数,它将在字符串元组中插入空格,以便所有字符串在len中相等。我还有一个函数,它接受字符串元组和一些格式化信息,并将它们组合成一个字符串元组

#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

我已经尝试将值存储在变量中,但没有改变。这很简单,但我看不到它。

2 个答案:

答案 0 :(得分: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 :(得分:0)

修改

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

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