简体   繁体   中英

Check if condition matches in python

Having trouble figuring out my list comprehension in 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.

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. No matter what numbers I put into my list (rolls), this condition still matches 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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