试图为if语句提供两个条件

时间:2017-10-29 00:09:44

标签: python-3.6

每当我运行此代码时,它总是转到else语句。如果满足符号兼容的条件,则输出将始终不兼容。我究竟做错了什么? sign和sign2是十二生肖的变量。我正在使用黄道带标志元素来查看两个标志是否相互兼容。

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)

print(finalResult)

1 个答案:

答案 0 :(得分:0)

我认为你想要做的是:

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

您的代码目前所做的是通过每个if语句。如果满足其中一个或多个条件,它将写入result,然后再次重写它。它进入else statement的原因是因为它不是在地球上的标志而且sign2不在水中。

我认为你想要的是,如果没有满足任何条件,则转到else语句。这可以通过上面输入的结构来实现。

[以下是更多信息] 1关于if语句及其对应方的工作方式。