布尔变量面试题

学习自:https://coolshell.cn/articles/2514.html

 

题目:“如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true

 

方法一

def func(a,b,c):
    if (a and b) or (b and c) or (c and a):
        return True
    else:
        return False

res = func(True,False,False) 
print(res)

 

方法一改进版

def func(a,b,c):
    return (a and b) or (b and c) or (c and a)

res = func(False,False,True)
print(res)

  

方法二

def func(a,b,c):
    res = (1 if a else 0) + (1 if b else 0) + (1 if c else 0)
    return True if res >= 2 else False

res = func(True,True,False)
print(res)

  

 

posted @ 2018-01-13 21:22  青山应回首  阅读(202)  评论(0编辑  收藏  举报