让错误语句通过python的If语句
问题 If语句允许条件通过,即使它不符合参数 说明 这段代码应该在选择等于列表操作的索引2或列表的索引3时运行。然而,当选择等于列表操作的索引1时,代码仍然运行让错误语句通过python的If语句,python,if-statement,indexing,Python,If Statement,Indexing,问题 If语句允许条件通过,即使它不符合参数 说明 这段代码应该在选择等于列表操作的索引2或列表的索引3时运行。然而,当选择等于列表操作的索引1时,代码仍然运行 if v.selection == v.operations[2] or v.operations[3]: print('arrived at a sub n missing - geometric') print('index of ', v.selection, 'in the list is ', v.operat
if v.selection == v.operations[2] or v.operations[3]:
print('arrived at a sub n missing - geometric')
print('index of ', v.selection, 'in the list is ', v.operations.index(v.selection))
if not v.aSub1:
v.aSub1 = input('first value in sequence>>>')
if not v.r:
v.r = input('common rate>>>')
if not v.n:
v.n = input('index (n)>>>')
pass
pass
pass
您从错误的角度看待if语句。 您所写的内容旨在: v.selection等于v.operations[2]或v.operations[3]都不是false。 就这点而言,任何不是0的东西都不是假的。 下面是一个例子:
a = 5;
b = 6;
c = 7;
if (a == b or c):
print("hey")
else:
print("nay")
其结果是:
hey
最好将输出放在这里,而不是图像。让人们更容易help@RayToal谢谢,明白了,谢谢你point@RayToal有一件事,使用
{v.operations….}
和(v.operations…...)
之间是否存在性能差异?两个元素的集合与两个元素的元组之间可能没有显著差异,差异可以忽略不计。如果您担心性能,您总是可以说v.selection==v.operations[2]或v.selection==v.operations[3]
,但通常不值得这么做。