[英]What is wrong with this python if statement?
I was making a function in python and used the following code:我在 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
but for a reason that I cannot figure out, on the line if(bool(_pretty) == True):
I am getting a syntax error on the colon.但出于我无法弄清楚的原因,在if(bool(_pretty) == True):
我在冒号上遇到语法错误。 I have tried making sure that the spacing is correct, that there are no issues with parenthesis being open, and have also tried rewriting it to make sure I wasn't missing something, but nothing has worked.我尝试确保间距正确,括号打开没有问题,并且还尝试重写它以确保我没有遗漏任何东西,但没有任何效果。 If somebody could help that would be great!如果有人可以提供帮助,那就太好了!
edit: Sorry!编辑:对不起! I did not realize that there was still an unclosed pair of parenthesis.我没有意识到还有一对未闭合的括号。 That is my bad...那是我的坏...
You're missing a )
in result.append(data[i][1]
causing the interpreter to get a little confused.您在result.append(data[i][1]
缺少)
导致解释器有点困惑。
Other issues...其他事宜...
You can use for
to iterate directly through the items in a list.您可以使用for
直接遍历列表中的项目。
No parens are required on if
conditions. if
条件不需要括号。
There's no need to cast _pretty
nor compare it to True.无需_pretty
或将其与 True 进行比较。 It's enough that _pretty
is a true value . _pretty
是一个真正的 value 就足够了。
The result formatting can be done with join
.结果格式化可以通过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.