2017-07-31 6 views
4
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文のどちらかが真である場合にのみ印刷したいと思います。変数が3と5の倍数になる可能性があるので、if、elif、elseを使用したくない。Pythonの複数のIfを相互に排他的に使用しないでください

+2

'%a3と%5:'ならば? –

答えて

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の計算モジュロを回避します。

あなたは、より多くの約数を追加、コピー/ペーストを回避し、ループ内でテストを考慮したい場合は(あなたがforrangeを持っているときwhileループを使用して、なぜところで?):

私のコメント時に拡大
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.") 
+3

はい!私はこれをコメントするつもりでしたが、あなたは私にそれを打ち負かしました。 –

+0

私は同じことを書いていました。 –

+0

@EricDuminil:それは本当です。しかし、その場合には副作用があります:一致する除数を 'print'する必要があります。 –

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 % xFalseであり、0です。真理値0と1を使用して、条件付きの結果を決定します。


わずか最適化:

if not a % 3: 
    ... 

if not a % 5: 
    ... 

elif a % 3: 
    ... 

は、冗長なテストを防止するために、わずかに最後の3つのテストを凝縮します。


最終的には、算術演算を1回少なく実行するので、フラグはより良いと思います。 (しばらく/スワップを除く)オリジナルの風味を保持

+1

わずかな最適化: '%5ではない場合:... elif a%3:'(5で再テストする必要はありません。その場合) –

+0

@ Jean-FrançoisFabreありがとう、よく見えます。 –

1

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." 
2

別のオプションは、3ためモジュロと5共に「truthy」であるかどうかをテストすることです。その場合、a3または5の倍数ではないことがわかります。

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.") 
+0

mod3、mod5 :) .. –

+0

@cᴏʟᴅsᴘᴇᴇᴅはい、ありがとう:-) –

+0

それは良いです!あなたは 'a'をインクリメントするのを忘れたことを除いて。そしてループは実際には 'a'を出力するはずです。 –

2

あなたは統合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を使用してテストします。

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)) 

7と11を考慮する場合は、primes = [3, 5, 7, 11]と定義してください。

関連する問題