检查python中的条件是否匹配

时间:2017-09-28 15:58:04

标签: python list-comprehension

无法在python中找出我的列表理解。我有三个条件,我正在寻找,我知道如何做其中两个,但其中一个条件似乎没有用。

我的条件是:

  1. 如果我的列表中的所有数字都相同且都是特定数字,则添加点
  2. 如果我的列表中的所有数字都相同,但它们不等于特定数字,那么请执行其他操作
  3. 如果列表中的数字不匹配,但它们等于特定数字而不是其他数字。
  4. 我有1个工作,我知道如何做3号,但我不能让2号工作正常。无论我将哪些数字放入我的列表(roll),这个条件仍然匹配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
    

2 个答案:

答案 0 :(得分:1)

这样的事可以让你到那里......

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

specificNum = 6

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

答案 1 :(得分:0)

    #imports

    import random

    #variables

    Roll_1_return = False
    Roll_2_return = False
    round_score = ROUND_TOTAL

    #assuming you only want to roll twice

    def Rolls():
        Roll_1 = random.randrange(1, 10)
        Roll_2 = random.randrange(1, 10)
        While True:
            if Roll_1 == 3:
                Roll_1_return = True
                return Roll_1_return
                break
            else:
                break
        While True:
            if Roll_2 == 7:
                Roll_2_return = True
                return Roll_2_return
                break
            else: 
                break

    Rolls()

    if Roll_1_return == True:
        print('Roll 1 is correct!')
        round_score + 25
    else:
        print('Roll 1 is incorrect..')

    if Roll_2_return == True:
        print('Roll 2 is correct!')
        round_score + 25
    else: 
        print('Roll 2 is incorrect..')

    if round_score == 50:
        print('You won $100!')
    elif round_score == 25:
        print('You won $50!')
    else:
        print('Too bad, you lost!')

如果我理解正确,这应该会给你你需要的东西!如果这不是你想要的,那就不要贬低我了!我尽力了解。