2017-10-29 63 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