2017-10-29 10 views
0

このコードを実行するたびに、常にelseステートメントに進みます。互換性のある標識の条件が満たされている場合、出力は常に互換性がありません。私は間違って何をしていますか? signとsign2は、黄道帯の記号の変数です。私は2つの兆候がお互いに互換性があるかどうかを確認するために十二支の要素を使用しています。ifステートメントの2つの条件を試す

def zodiacCompatibility(sign, sign2): 
fire = ["Aries, Leo, Sagittarius"] 
earth = ["Capricorn, Taurus, Virgo"] 
water = ["Pisces, Cancer, Scorpio"] 
air = ["Gemini, Aquarius, Libra"] 


if (sign in fire and sign2 in fire): 
    result = ("The two signs are compatible") 
if (sign in earth and sign2 in earth): 
    result = ("The two signs are compatible") 
if (sign in air and sign2 in air): 
    result = ("The two signs are compatible") 
if (sign in water and sign2 in water): 
    result = ("The two signs are compatible") 
if (sign in fire and sign2 in air): 
    result = ("The two signs are compatible") 
if (sign in water and sign2 in earth): 
    result = ("The two signs are compatible") 
if (sign in air and sign2 in fire): 
    result = ("The two signs are compatible") 
if (sign in earth and sign2 in water): 
    result = ("The two signs are compatible") 
else: 
    result = ("The two signs are not compatible") 

finalResult = zodiacCompatibility(sign, sign2) 

プリント(finalResult)

答えて

0

私は何がやりたいことはあると思う:

if (condition): 
elif (condition): 
elif (condition): 
. 
. 
. 
else: 

何あなたのコードは現在ありませんがif文それぞれを通過しています。これらの条件の1つまたは複数が満たされている場合は、resultに書き込んでから、再度書き換えます。 else statementに行く理由は、標識が地球になく、標識2が水中にないためです。

私が望むと思うのは、条件が満たされていない場合、else文に行きます。これは、私が上にタイプした構造に達成することができます。

文とその対応がどのように働くかの場合に1 [ここでより多くの情報があります]。

+0

私はエリフで条件を1つ、またはその中にある場合は入れ子にすることに決めました。出来た。どうもありがとう。 –

+0

しかし、なぜ "と"は動作しませんでしたか?満たされる必要がある2つの条件がある場合には、 "と"は使用されません。 –

+0

「と」条件の両方またはいずれかが実行されます文の中でどんな満たされている場合は、条件付きで作業を行います。 – PeskyPotato

関連する問題