这个 python if 语句有什么问题?

What is wrong with this python if statement?

我在 python 中创建了一个函数并使用了以下代码:

def multi(_conv, _pretty = False):
  result = []
  newResult = ""
  for a in range(len(_conv)):
    for i in range(len(data)):
      if (str(_conv[a]) == data[i][0]):
        result.append(data[i][1]

  if(bool(_pretty) == True):
    for i in range(len(result)):
      newResult += str(result[i])
      if(i != len(result) - 1):
        newResult += ", "
    return newResult

但是由于我无法弄清楚的原因,在 if(bool(_pretty) == True): 行中,我在冒号上遇到语法错误。我已经尝试确保间距正确,打开括号没有问题,并且还尝试重写它以确保我没有遗漏任何东西,但没有任何效果。如果有人能提供帮助那就太好了!

编辑:抱歉!我没有意识到还有一对未闭合的括号。那是我的错...

您在 result.append(data[i][1] 中缺少一个 ) 导致解释器有点混乱。


其他问题...

您可以使用 for 直接遍历列表中的项目。

if 条件下不需要括号。

无需强制转换 _pretty 也无需将其与 True 进行比较。 _prettya true value.

就够了

结果格式可以用join完成。

  for c in _conv:
    for d in data:
      if str(c) == d[0]:
        result.append(d[1])

  if _pretty:
    return ", ".join(result)