""" File: w13SampleSolution.py Author: Ben Schafer Comments: This is ONE possible solution for the calculateScore() activity. """ def containsStraight(dice): dice.sort() if len(dice)!=6: return False flag=True for index in range(6): if dice[index]!=index+1: flag=False return flag def containsThreePairs(dice): dice.sort() if len(dice)!=6: return False flag=True for index in range(0,5,2): if dice[index]!=dice[index+1]: flag=False return flag def countSingles(dice): localScore = 0 for index in range(len(dice)): if dice[index]==1: localScore+=100 if dice[index]==5: localScore+=50 return localScore def calculateScore(original): dice = original.copy() #this code changes the list so I want to make a copy dice.sort() #it is easier to check for things if the dice are sorted if containsStraight(dice): return 3000 if containsThreePairs(dice): return 1500 score=0 #contains triples? #Done here rather than in a helper method because of changes I make index=0 while len(dice)>index+2: if dice[index]==dice[index+1] and dice[index]==dice[index+2]: if dice[index]==1: score+=1000 else: score+=dice[index]*100 for x in range(3): dice.pop(index) #remove these three dice so you don't double count else: index=index+1 #contains singles? score = score + countSingles(dice) return score