Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法简化这个while条件(python)_Python_While Loop_Conditional Statements - Fatal编程技术网

有没有办法简化这个while条件(python)

有没有办法简化这个while条件(python),python,while-loop,conditional-statements,Python,While Loop,Conditional Statements,我一直在寻找一种简化的方法 while bluePoints < BOn // 2 + 1 and redPoints < BOn // 2 + 1: 蓝点bool: 返回蓝色p

我一直在寻找一种简化的方法

while bluePoints < BOn // 2 + 1 and redPoints < BOn // 2 + 1:
蓝点
我知道如何用字符串和列表来简化,但不能用int来简化。您应该澄清“简化此while条件的方法”的含义。这可以用多种方式来解释,您要寻找的答案可能取决于代码其余部分中发生的情况。也就是说,这里有几个可供选择的条件,它们会导致相同的bool:

def bon_calc(bon : int) -> int:
    return bon // 2 + 1

def red_blu_check(red_p : int, blu_p : int, bon : int) -> bool:
    return blu_p < bon // 2 + 1 and red_p < bon // 2 + 1

bluePoints = 1
redPoints = 1
BOn = 2

# Your orignal function
con1 = bluePoints < BOn // 2 + 1 and redPoints < BOn // 2 + 1

# Using all instead of and
con2 = all([bluePoints < BOn // 2 + 1, redPoints < BOn // 2 + 1])

# Using custom function bon_calc
con3 = bluePoints < bon_calc(BOn) and redPoints < bon_calc(BOn)

# Using custom function red_blu_check
con4 = red_blu_check(redPoints, bluePoints, BOn)

assert con1 == con2 == con3 == con4

def bon_calc(bon:int)->int:
返回bon//2+1
def red_blu_check(red_p:int,blu p:int,bon:int)->bool:
返回蓝色p

与其他受访者一样,我不确定自己是否知道你所说的简化是什么意思。我想你不喜欢这种重复

bluePoints < BOn // 2 + 1 and redPoints < BOn // 2 + 1
bluePoints
如果这就是问题所在,您可以使用辅助变量来存储重复计算的结果

limit = BOn // 2 + 1
while bluePoints < limit and redPoints < limit:
limit=BOn//2+1
蓝点<限制,红点<限制:

有几种方法可以将
while
子句中的布尔条件从两个减少到一个(
max()
all()
让我想起),但我想不出比
有多大改进的方法。Python中的布尔运算符是快捷运算符,因此在任何情况下,只有在第一个条件为false时,才会测试第二个条件。

欢迎使用堆栈溢出!请再添加一些内容:如何用字符串和列表简化它,您已经做了什么。。。非常感谢。嗨,你能解释一下简化是什么意思吗?