繁体   English   中英

这个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 进行比较。 _pretty一个真正的 value 就足够

结果格式化可以通过join完成。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM