Python条件语句

Dic*_*ier 1 python conditional conditional-statements

def Fitness(a, b, c):  
    if ((a&b&c) >= 4) & ((a+b+c) >= 13):  
        return('Gold')  
    if ((a&b&c) >= 3) & ((a+b+c) >= 10):  
        return('Silver')   
    if ((a&b&c) >= 2) & ((a+b+c) >= 07):  
        return('Pass')  
    else:  
        return('Fail')
Run Code Online (Sandbox Code Playgroud)

现在问题是什么时候Fitness(2,2,5)给出,控制跳转到默认即.'失败'.实际输出在哪里是'通过'.?

How*_*ard 17

注意

a&b&c >= 2
Run Code Online (Sandbox Code Playgroud)

不同于

a>=2 and b>=2 and c>=2.
Run Code Online (Sandbox Code Playgroud)

我认为你的意思是第二个,即所有值都大于2.(第一个使用二进制文件并使用所有值,并将其与值二进行比较.)

  • 啊! 今天最好使用水晶球+1! (4认同)

Tim*_*ker 5

使用and而不是&(二进制和).并且不要写07- 0根据您的Python版本,以... 开头的数字可能被解释为八进制.

再加上霍华德的灵感,我建议:

def Fitness(a, b, c):
    if all(x>=4 for x in (a,b,c)) and (a+b+c) >= 13:
        return('Gold')
    if all(x>=3 for x in (a,b,c)) and (a+b+c) >= 10:
        return('Silver') 
    if all(x>=2 for x in (a,b,c)) and (a+b+c) >= 7:
        return('Pass')
    return('Fail')
Run Code Online (Sandbox Code Playgroud)

你还没有获得铜牌,这让人感到很难过......