2017-10-29 75 views
0

每當我運行此代碼的兩個條件,它總是轉到else語句。如果滿足符號的條件,則輸出將始終不兼容。我究竟做錯了什麼? 標誌和SIGN2是十二生肖的變量。我使用星座元素來查看兩個符號是否相互兼容。試圖有一個if語句

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語句。如果一個或多個這些條件都滿足將會寫入result,然後在它再次重新寫。它進入else statement的原因是因爲它的標誌是不是在地球和SIGN2是不溶於水。

我想你想的是,如果沒有一個條件滿足它轉到else語句。這可以通過上面輸入的結構來實現。

[以下是詳細信息]如何if語句和他們的同行攜手1

+0

我決定在elif中有一個條件,或者if和nest在裏面。有效。非常感謝。 –

+0

但爲什麼沒有「和」的工作?如果需要滿足兩個條件,則不使用「和」。 –

+0

「和」在條件語句中起作用,如果兩個條件都滿足或兩個條件都滿足,則語句中的任何內容都會被執行。 – PeskyPotato