a=[3,90,3,7,8,100] # in python lists are denoted by square brackets
if sum(a)<100: # Here sum(a) means sum([3,90,...]) it's a list and it can add all.
a.append(2) # append means adding new element at end of the list a
print(a)
a=[3,90,3,7,8,100]
if [sum(a)<100]: # here the condition will be always true because [True] is not empty
a.append(2)
print(a)
因此,当您使用方括号时,python会检查列表是否为空,而不是列表中的值
这总是被解释为正确的,因此结果是错误的
强调这一点的例子:
两个案例都将打印“test”
检查列表是否为空。请参见下面的说明
相关问题 更多 >
编程相关推荐