I have this question for a practice assignment:
In Cee-lo, having a roll where two of the three dice have the same value is called a point. This is the second weakest dice roll, only stronger than a 1-2-3. Complete the is_point() function that takes a single string parameter dice_str consisting of three dice values. If the dice rolled (as represented by the dice_str) have two of the three dice with the same value, the function should return True. Otherwise, the function should return False.
def is_point(dice_str): dice_str = "0" + dice_str # 0123 count = 1 while count < 7: count = str(count) index_zero = dice_str.find(count, 0, 2) from_index1 = dice_str.find(count, 2, 3) print(dice_str[index_zero]) print(dice_str[from_index1]) if index_zero == -1 or from_index1 == -1: count = int(count) + 1 print(count) else: if dice_str[index_zero] == dice_str[from_index1]: return True return False
In my code, I use the first if function to test whether the count number does not appear twice. However, it seems to always be executing. For example, if my number is 141, it will still execute the if function and add the count by one even though that is not what I want it to do. I have tried printing the index_zero and from_index1 values and they are indeed the same number. So why is the if function not executing?
An easier strategy is to convert your string into a set which gets rid of duplicates. If the length of the set is 2 (not 1 or 3), then you have exactly 2 identical numbers:
def is_point(dice_str): return len(set(dice_str)) == 2 print(is_point('122')) #True print(is_point('136')) #False print(is_point('666')) #False