#program to play a 2-player dice game import random #subroutine to get and validate menu choice def menuChoice (): print("\nOption 1: Display rules") print("Option 2: Start new game") print("Option 3: Quit") choice = input("What would you like to do? ") while choice <"1" or choice>"3": print ("That is not a valid choice.") choice =(input ("Please enter a number between 1 and 3: ")) return (choice) #subprogram to display rules def displayRules(): print( """ \nThe rules of the game are as follows:\n Players take turns to throw two dice. If the throw is a ‘double’, i.e. two 2s, two, threes, etc., the player’s score reverts to zero and their turn ends. If the throw is not a ‘double’, the total shown on the two dice is added to the player’s score. A player may have as many throws as they like in any turn until they either throw a double or pass the dice. The first player to reach a score of 50 wins the game. """ ) # subprogram for each player to take a turn def playerTurn(player, score): print ("\n\nYour turn, ", player) anotherGo = "Y" scoreThisTurn = 0 while anotherGo=="Y" or anotherGo =="y": die1 = random.randint(1,6) die2 = random.randint(1,6) print ("You rolled", die1,"and", die2) if die1==die2: scoreThisTurn = 0 cumulativeScore = 0 print("Bad luck! Press Enter to continue") wait = input() anotherGo = "N" else: scoreThisTurn = scoreThisTurn + die1 + die2 cumulativeScore = score + scoreThisTurn print ("\nYour score this turn is",scoreThisTurn) print(player, "Your cumulative score is...",cumulativeScore) if cumulativeScore>=50: return(cumulativeScore) print("\nAnother go? (Answer Y or N)") anotherGo = input() return (cumulativeScore) #end turn #subprogram to play game def playGame(): score1=0 score2=0 player1 = input("enter Player1's name: ") player2 = input("enter Player2's name: ") while score1<50 and score2<50: totalScore = playerTurn(player1, score1) score1 = totalScore if score1>=50: print("You win!") else: totalScore = playerTurn(player2, score2) score2=totalScore if score2>=50: print("You win!") #main prog option = menuChoice() while int(option)!=3: if int(option) == 1: displayRules() else: playGame() option = menuChoice() print ("\nGoodbye!") #end of program