python - 检查 python 中的条件是否匹配

标签 python list-comprehension

无法弄清楚我在 python 中的列表理解。我正在寻找 3 个条件,并且我知道如何实现其中两个,但其中一个条件似乎不起作用。

我的条件是:

  1. 如果我列表中的所有数字都相同并且都是特定数字,则加分
  2. 如果列表中的所有数字都相同,但不等于特定数字,则执行其他操作
  3. 如果列表中的数字不匹配,但它们等于特定数字,则执行其他操作。

我有 1 工作,并且我知道如何做 3,但我无法让 2 正常工作。无论我将什么数字放入列表(滚动)中,此条件仍然匹配 True。有人可以帮忙吗?这是我当前的代码:

def check_conditions(rolls, round_number):
    """
    Check if number on rolled die matches one of three conditions
    :param rolls:
    :param round_number:
    :return round:
    """
    round_score = ROUND_TOTAL
    rolls = str(rolls)

    bunco = all(roll == ROUND_NUMBER for roll in rolls)
    mini_bunco = all(roll == roll[0] and roll != ROUND_NUMBER for roll in rolls)

    if bunco == True:
        print("BUNCO!")
        round_score += 20
    elif mini_bunco == True:
        print("MINI-BUNCO!")
        round_score += 5
    else:
        pass

    return round_score

输出:

Starting Round Number 1
You rolled: [2, 3, 3]
MINI-BUNCO!
Points this round: 5

最佳答案

这样的事情应该可以帮助你......

rolls = [5,5,5,5,5,5]

specificNum = 6

 if len(set(rolls)) == 1:
     if rolls[0] != specificNum:
         print 'Do something'

关于python - 检查 python 中的条件是否匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46473557/

相关文章:

python - Selenium - 在检查 HTML 之前找不到可见元素?

python - 在 Pandas 数据框中检索 .loc 索引值

python - 如何存储 wiki 站点(vcs)

python - Durand-kerner 实现不起作用

Python:改进长累加和

python - 给我一些很酷的 python 列表理解

python - 列表理解错误 "IndexError: list index out of range "

python - 空路径与其中任何一个都不匹配 - Django 2 By Examples

python - 遍历元组列表并仅解压缩第一个元素

Haskell 泛化问题(涉及列表理解)