如果语句让错误的语句通过python

2024-09-27 00:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

如果语句允许一个条件通过,即使它不符合参数

描述

当所选内容等于列表操作的索引2或列表的索引3时,应该运行这段代码。然而,当selection等于列表操作的索引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.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

output


Tags: 代码in列表input参数indexifnot
1条回答
网友
1楼 · 发布于 2024-09-27 00:22:45

你从错误的角度看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

相关问题 更多 >

    热门问题