1

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
Justin Boucher
  • 343
  • 4
  • 17
  • So that would be if all numbers are equal to the first number, but not ROUND_NUMBER? – jonrsharpe Sep 28 '17 at 16:01
  • Hint: First check that all the numbers in `rolls` are identical to `rolls[0]`, and if that's true, then test if `rolls[0] == ROUND_NUMBER`. – PM 2Ring Sep 28 '17 at 16:02
  • Use what's suggested [here](https://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical) and then as @PM2Ring suggested, check if rolls[0] != ROUND_NUMBER – ShreyasG Sep 28 '17 at 16:04
  • `mini_bunco = all(roll == roll[0] and roll != ROUND_NUMBER for roll in rolls)` is what I have now, but I'm getting a TypeError: 'int' object is not subscriptable – Justin Boucher Sep 28 '17 at 16:06
  • Updated code in initial question and provided output – Justin Boucher Sep 28 '17 at 16:11

2 Answers2

1

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'
Tim Gottgetreu
  • 483
  • 1
  • 8
  • 21
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!')

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.

EgMusic
  • 136
  • 17