Write Python Code for a dice game. 2 players and a die are required. Rules: 1) R
ID: 3550357 • Letter: W
Question
Write Python Code for a dice game. 2 players and a die are required.
Rules:
1) Roll the Dice.
2) If user rolled a 1:
The pot gets set to zero
The other player goes to step 1.
3) Else roll of 2-6 is added to the pot.
4) User can choose to hold or roll again.
Choice ROLL= Return to Step 1.
Choice HOLD=
-Increment Player score by the pot amount.
-Pot gets set to 0.
-second player gets to roll and goes to Step 1.
Explanation / Answer
For starters, there's no need to pass the players' names to the function which is supposed to get the players' names. Secondly, "inputNames" is such a dry, overly technical name. Let's make it simpler and friendlier: def get_names(): playerOne = raw_input("Enter the first player's name: ") playerTwo = raw_input("Enter the second player's name: ") return playerOne, playerTwo Now let's write a function to roll a single dice. Single die. Whatever. import random # We need Python's random number generator. def dice(sides=6): return random.randint(1, sides) This uses a *default value* for the number of sides. If you want to roll a ten-sided die, you call: dice(10) and it will give back a random number between 1 and 10. But if you want to roll the default six-sided die, you can leave out the 6 and just write: dice() and it will give back a random number between 1 and 10. Let's write a small program to play dice between two players. We'll make a simple dice game function that returns the name of the winning player, or the empty string in the case of a draw. def dice_game(playerOne, playerTwo): # playerOne rolls a single six-sided die. rollOne = dice() # So does playerTwo. rollTwo = dice() # The winner is the one who gets a higher number. if rollOne > rollTwo: return playerOne elif rollTwo > rollOne: return playerTwo else: # They must have rolled equal, so a draw. return "" Putting it all together, copy the lines between the ###### into a new file and save it as a Python program: ###### import random def dice(sides=6): return random.randint(1, sides) def get_names(): playerOne = raw_input("Enter the first player's name: ") playerTwo = raw_input("Enter the second player's name: ") return playerOne, playerTwo def dice_game(playerOne, playerTwo): # playerOne rolls a single six-sided die. rollOne = dice() # So does playerTwo. rollTwo = dice() # The winner is the one who gets a higher number. if rollOne > rollTwo: return playerOne elif rollTwo > rollOne: return playerTwo else: # They must have rolled equal, so a draw. return "" def displayWinner(winner): print "And the winner is: ", if winner == "": print "... the game is a draw!" else: print winner print def main(): playerOne, playerTwo = get_names() play_again = True while play_again: print "Rolling dice..." winner = dice_game(playerOne, playerTwo) displayWinner(winner) answer = raw_input("Would you like to play again? (y/n) ") play_again = answer.strip().lower() in ('y', 'yes') main() ###### Have a play around with that, and feel free to ask any questions you like.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.