在嵌套的if循环中,如何为多个匹配条件返回true?

时间:2017-07-04 14:04:29

标签: python python-3.x

在下面的程序中,即使所有if条件都匹配,它只返回true一次。如何让它返回true并按条件匹配打印多次?

lotto_numbers = [1,1,1]
fireball_number = 1
user_input1 = user_input2 = user_input3 = 1

def fbcheck():
    if lotto_numbers == [user_input1,user_input2,fireball_number]:
        return True
    elif lotto_numbers == [fireball_number, user_input2, user_input3]:
        return True
    elif lotto_numbers == [user_input1, fireball_number, user_input3]:
        return True
    else:
        return False

if (fbcheck() == True):
    print ('you won')

1 个答案:

答案 0 :(得分:1)

您可以使用all

def fbcheck():
    user_data = [user_input1,user_input2,fireball_number]

    lotto_numbers = [1,1,1]

    print([a==b for a, b in zip(lotto_numbers, user_data)])

    return all(a==b for a, b in zip(lotto_numbers, user_data))



print(fbcheck())

输出:

[True, True, True]

True