猿问

SyntaxError:语法无效(Python 3.2)

我有一个函数,它将在字符串元组中插入空格,以便所有字符串的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

我试图将值存储在变量中,但没有任何改变。大概有点简单,但我看不到。


料青山看我应如是
浏览 197回答 2
2回答

12345678_0001

您错过了)此行的结束语:retstring = retstring + ("&nbsp;"*(max - len(string)+1,))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;编辑:在您的代码:>>> 'retstring = retstring + ("&nbsp;"*(max - len(string)+1,)'.count("(")3>>> 'retstring = retstring + ("&nbsp;"*(max - len(string)+1,)'.count(")")2
随时随地看视频慕课网APP

相关分类

Python
我要回答