简体   繁体   English

检查条件是否在python中匹配

[英]Check if condition matches in python

Having trouble figuring out my list comprehension in python.无法弄清楚我在 python 中的列表理解。 I have 3 conditions that I'm looking for, and I know how to do two of them, but one of the conditions doesn't seem to work right.我有 3 个要查找的条件,我知道如何执行其中的两个条件,但其中一个条件似乎不起作用。

My conditions are:我的条件是:

  1. If all the numbers in my list are the same and they are all a specific number, then add points如果我列表中的所有数字都相同并且都是特定数字,则加分
  2. If all numbers in my list are the same but they do not equal a specific number then do something else如果我列表中的所有数字都相同但不等于特定数字,则执行其他操作
  3. If numbers in list do not match, but they equal a specific number than do something else.如果列表中的数字不匹配,但它们等于特定数字而不是做其他事情。

I have 1 working, and I know how to do number 3, but I can't get number 2 working properly.我有 1 个工作,我知道如何做第 3 个,但我无法让第 2 个正常工作。 No matter what numbers I put into my list (rolls), this condition still matches True.无论我把什么数字放入我的列表(滚动),这个条件仍然匹配 True。 Can someone please assist?有人可以帮忙吗? Here is my current code:这是我当前的代码:

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

OUTPUT:输出:

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

Something like this should get you there...像这样的事情应该让你到达那里......

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

specificNum = 6

 if len(set(rolls)) == 1:
     if rolls[0] != specificNum:
         print 'Do something'
    #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!')

If I understand correctly, this should give you what you need!如果我理解正确,这应该给你你所需要的! If this is not what you wanted, plz do not downvote me!如果这不是您想要的,请不要对我投反对票! I tried my hardest to understand.我尽力去理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM