2

Most of my code works perfectly, the only one I cannot figure out is three of a kind. If three of the same number appears it adds together to get the score. Here is that section of code as I have it right now.

def threeOfOne(dicelst):
    total = 0
    for die in dicelst:
        if dicelst[0] == dicelst[1:3]:
            total += die
        elif dicelst[1] == dicelst[2:5]:
            total += die
        else:
            total = 0
        return total

I feel like I am missing something very simple but I can't get it to work it always shows zero.

ChelluhC
  • 37
  • 6

1 Answers1

0

In your function, you are checking if a single value is equivalent to a list!

>>> dicelst = [1,3,5,4,2]
>>> dicelst[0]
1
>>> dicelst[1:3]
[3, 5]

Try checking the count of each die in the list

def threeOfOne(dicelst):
    for die in dicelst:
       if dicelst.count(die) >= 3:  # only if there are at least three matches
           return die * dicelst.count(die)
    return 0

Here is an expression which I believe solves your problem

def threeOfOne(dicelst):
    return sum(x for x in dicelst if dicelst.count(x) >= 3)

This generates all the values in dicelst which appear at least three times

  • (x for x in y) generator expression which iterates over y
  • dicelst.count(x) counts the number of times x appears in dicelst
  • sum(iterable) add everything in the contained list (or other iterable)

If you require exactly three of a kind, just check if the count == 3

ti7
  • 16,375
  • 6
  • 40
  • 68