import random #A function to simulate the roll of a die #input: none #output: random int between 1 and 6 def roll(): return random.randint(1, 6) #A function to prompt user for a name #input: string "first" or "second" representing player number #output: name def getName(num): prompt = "Enter name of " + num + " player: " name = raw_input(prompt) return name #A function to print "Let's play!" #input: none #output: none def printStartMsg(): print "Let's play!" #A function to print "Thanks for playing!" #input: none #output: none def printEndMsg(): print "Thanks for playing!" #A function to ask the user if he/she wants to play again #input: none #output: True if the user wants to play again, False otherwise def playAgain(): playagain = raw_input("Shall we play again? [y/n] ") if playagain == "y": return True else: return False #A function to print the player name and value he/she rolled #input: name, int value of the roll #output: none def printRoll(pname, rv): print pname + " you rolled a " + (str(rv)) #A function to determine the winner of the round #input: value rolled by player 1 and value rolled by player 2 #output: 1 if player 1 won, 2 if player 2 won, 3 for tie def determineWinner(p1val, p2val): if p1val > p2val: return 1 elif p2val > p1val: return 2 else: return 0 #A function to print a "congrats" message for the winner of the round. #input: int representing the winner (1, 2, or 3 for tie), player 1 name, player 2 name #output:none def printWinner(winner, p1name, p2name): if winner == 1: print "Congrats " + p1name + "! You are the winner!" elif winner == 2: print "Congrats " + p2name + "! You are the winner!" else: print "It's a tie!" #A function containing the main logic of game play #input: player 1 name and player 2 name #output: none #Algorithm: #print start message #ask if user wants to play again #while the user wants to play again # roll for player 1 # print player 1's roll # roll for player 2 # print player 2's roll # determine the winner # print a message for the winner # ask if user wants to play again def play(p1name, p2name): printStartMsg() pa = playAgain() while pa == True: p1rollValue = roll() printRoll(p1name, p1rollValue) p2rollValue = roll() printRoll(p2name, p2rollValue) winner = determineWinner(p1rollValue, p2rollValue) printWinner(winner, p1name, p2name) pa = playAgain() printEndMsg() #main logic to begin game #ask for names #call play function player1name = getName("first") player2name = getName("second") play(player1name, player2name)