在python中使用多个不相互排斥的Ifs

时间:2017-07-31 20:25:51

标签: python if-statement

a=0
while a<30:
    a+=1
    print(a)
    if a%3==0:
        print("This is a multiple of 3.")
    if a%5==0:
        print("This is a multiple of 5.")
    else:
        print("This is not a multiple of 3 or 5.")

我希望这个else语句只在前面的if语句中的NEITHER为真时才打印。我不想使用if,elif,else因为变量可能是3和5的倍数。

5 个答案:

答案 0 :(得分:6)

如果其中一个条件匹配,您可以设置一个标志。如果两次测试后该标志仍为False,则打印后备消息:

a=0
while a<30:
    a+=1
    print(a)
    match = False
    if a%3==0:
        print("This is a multiple of 3.")
        match = True
    if a%5==0:
        print("This is a multiple of 5.")
        match = True
    if not match:
        print("This is not a multiple of 3 or 5.") 

这种技术也避免了多次计算3和5的模数。

如果你想添加更多的除数,请避免复制/粘贴,并考虑循环测试(BTW为什么在whilefor时使用range循环?):

for a in range(1,31):
    print(a)
    match = False
    for i in [3,5]:
        if a%i==0:
            print("This is a multiple of {}.".format(i))
            match = True
    if not match:
        print("This is not a multiple of 3 or 5.")

答案 1 :(得分:2)

扩展我的评论:

if not a % 3:
    print("This is a multiple of 3.")

if not a % 5:
    print("This is a multiple of 5.")

if a % 3 and a % 5:
    print("Not a multiple of 3 or 5.")

如果某个数字可以整除,a % x为0,即False。我们使用0和1的真值来确定条件的结果。

轻微优化:

if not a % 3:
   ...

if not a % 5:
   ...

elif a % 3:
   ...

稍微调整最后3次测试,以防止进行冗余测试。

最后,我相信一个标志更好,因为你的算术运算少了一次。

答案 2 :(得分:2)

另一种选择是测试35的模数是否都是“真实的”。如果是,那么您知道a不是35的倍数。您还可以避免在循环开始时立即重新计算每个数字的模数,并在整个身体的其余部分使用结果:

a = 0
while a < 30:
    a += 1
    mod3, mod5 = a % 3, a % 5
    if not mod3:
        print("This is a multiple of 3.")
    if not mod5:
        print("This is a multiple of 5.")
    if mod3 and mod5:
        print("This is not a multiple of 3 or 5.") 

答案 3 :(得分:2)

您可以使用带有集成LD_LIBRARY_PATH的列表推导来获取除数列表。如果它为空,则可以打印if

这样,添加可能的除数并编写描述结果的句子更容易:

"This is not a multiple of ..."

输出:

N = 30
primes = [3, 5]
primes_str = ' or '.join(str(p) for p in primes)

for n in range(1, N + 1):
    divisors = [str(p) for p in primes if n % p == 0]
    if divisors:
        print("%d is a multiple of %s." % (n, ' and '.join(divisors)))
    else:
        print("%d is not a multiple of %s." % (n, primes_str))

如果您只想测试该号码是否不是倍数,可以使用1 is not a multiple of 3 or 5. 2 is not a multiple of 3 or 5. 3 is a multiple of 3. 4 is not a multiple of 3 or 5. 5 is a multiple of 5. 6 is a multiple of 3. 7 is not a multiple of 3 or 5. 8 is not a multiple of 3 or 5. 9 is a multiple of 3. 10 is a multiple of 5. 11 is not a multiple of 3 or 5. 12 is a multiple of 3. 13 is not a multiple of 3 or 5. 14 is not a multiple of 3 or 5. 15 is a multiple of 3 and 5. 16 is not a multiple of 3 or 5. 17 is not a multiple of 3 or 5. 18 is a multiple of 3. 19 is not a multiple of 3 or 5. 20 is a multiple of 5. 21 is a multiple of 3. 22 is not a multiple of 3 or 5. 23 is not a multiple of 3 or 5. 24 is a multiple of 3. 25 is a multiple of 5. 26 is not a multiple of 3 or 5. 27 is a multiple of 3. 28 is not a multiple of 3 or 5. 29 is not a multiple of 3 or 5. 30 is a multiple of 3 and 5. any

all

如果您想考虑7和11,只需定义for n in range(1, N + 1): if all(n % p for p in primes): print("%d is not a multiple of %s." % (n, primes_str)) for n in range(1, N + 1): if not any(n % p == 0 for p in primes): print("%d is not a multiple of %s." % (n, primes_str))

答案 4 :(得分:1)

保留原始风味(while / for swap除外):

for a in range(1, 31):
    print(a)

    if a % 3 == 0:
        print("This is a multiple of 3.")

        if a % 15 != 0:
            continue
    if a % 5 == 0:
        print("This is a multiple of 5.")
    else:
        print("This is not a multiple of 3 or 5."