尝试为 if 语句设置两个条件
Trying to have two conditions for an if statement
每当我 运行 这段代码时,它总是转到 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)
打印(最终结果)
我想你想做的是:
if (condition):
elif (condition):
elif (condition):
.
.
.
else:
您的代码当前所做的是遍历每个 if 语句。如果满足其中一个或多个条件,它将写入 result
,然后再次重写。它进入 else statement
的原因是因为它的符号不在地球上并且符号 2 不在水中。
我想你想要的是,如果满足 none 个条件,它就会转到 else 语句。这可以实现到我上面输入的结构。
[这里有更多信息]1 关于 if 语句及其对应物如何工作的信息。
每当我 运行 这段代码时,它总是转到 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)
打印(最终结果)
我想你想做的是:
if (condition):
elif (condition):
elif (condition):
.
.
.
else:
您的代码当前所做的是遍历每个 if 语句。如果满足其中一个或多个条件,它将写入 result
,然后再次重写。它进入 else statement
的原因是因为它的符号不在地球上并且符号 2 不在水中。
我想你想要的是,如果满足 none 个条件,它就会转到 else 语句。这可以实现到我上面输入的结构。
[这里有更多信息]1 关于 if 语句及其对应物如何工作的信息。