# 2 player dice game

This is a finished project where Two people roll a dice twice and if the total of the two dice are even it adds 10 points to the two dice rolls and if the total is negative it takes away 5 points from the two dice rolls.

import random
import time

total_score2 = 0
total_score1 = 0
rounds = 0
playerOnePoints = 0
playerTwoPoints = 0

while rounds < 5:
total_score2 = total_score2 + playerTwoPoints
total_score1 = total_score1 + playerOnePoints
rounds = rounds + 1
number = random.randint(1,6)
number2 = random.randint(1,6)
playerOnePoints = number + number2
print("Round",rounds)
print("-------------------------------------------")
print("Player 1's turn    Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
time.sleep(1)
print("Player 1's first roll is", number)
print("Player 1's second roll    Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
time.sleep(1)
print("player 1's second roll is", number2)
if playerOnePoints % 2 == 0:
playerOnePoints = playerOnePoints + 10
print("Player 1's total is even so + 10 points")
print("-------------------------------------------")
print("Player 1 has",playerOnePoints, "points")
else:
playerOnePoints = playerOnePoints - 5
print("player 1's total is odd so -5 points")
print("-------------------------------------------")
print("Player 1 has",playerOnePoints, "points")
number = random.randint(1,6)
number2 = random.randint(1,6)
playerTwoPoints = number + number2
print("-------------------------------------------")
print("Player 2's turn    Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
time.sleep(1)
print("Player 2's first roll is", number)
print("Player 2's second roll    Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
time.sleep(1)
print("player 2's second roll is", number2)
if playerTwoPoints <= 0:
playerTwoPoints = 0
if playerTwoPoints % 2 == 0:
playerTwoPoints = playerTwoPoints + 10
print("Player 2's total is even so + 10 points")
print("-------------------------------------------")
print("Player 2 has",playerTwoPoints, "points")
else:
playerTwoPoints = playerTwoPoints - 5
print("player 2's total is odd so -5 points")
print("-------------------------------------------")
print("Player 2 has",playerTwoPoints, "points")
print("-------------------------------------------")

print("Total score for player 1 is", total_score1)
print("-------------------------------------------")
print("Total score for player 2 is", total_score2)
print("-------------------------------------------")


As I mentioned this is a finished project but I would like suggestions on how I could improve this project to make it better also I would like it if someone could explain to me on how to shorten the code. All suggestions will be greatly appreciated

Thanks :-)

• Every contribution to this will help me alot so please make suggestions and as I said every contribution will be greatly appreciated – colkat406 Dec 3 '18 at 16:54

Some suggestions:

• Pulling out functions or classes w/methods will allow you to reduce the amount of nesting in the code, and at the same time will package the code into easily understood functionally and semantically separate parts. This is incredibly important in larger programs, but can also highlight ways in which existing shorter programs can be structured for maintainability. For example, a Player class could have a score field and a Game class could have a rounds field and a roll method:

MAX_ROUNDS = 5

class Player:
def __init__(self):
self.score = 0

def win(self):
self.score += 10

def lose(self):
self.score -= 5

class Game:
rounds = 0

player_1 = Player()
player_2 = Player()

def play():
if self.rounds >= MAX_ROUNDS:
…
else:
…
self.rounds += 1

• Dependency injection of randint and input would allow this code to be unit tested.

• If someone wants to play this a lot they would probably not appreciate the “dramatic pause” before showing the result of the rolls. I would simply get rid of the sleeps.
• Could you show me what you mean by when you said (a Player class could have a score field and a Game class could have a rounds field and a roll method.) – colkat406 Dec 3 '18 at 16:32
• if I was to put the code you wrote into my code where would I put it ? – colkat406 Dec 4 '18 at 17:28
• I'm afraid that's a bit more coaching than you can expect from this site. If you want to learn the basics of object orientation I'm afraid you'll have to look for a course or tutorial. – l0b0 Dec 4 '18 at 18:46
• ok then thanks for all of your help anyway :-) – colkat406 Dec 4 '18 at 20:36
• Isn't that code declaring rounds, score, etc as class variables, and not instance variables? Wouldn't all instances of Player be sharing the same score? – eric.m Jul 5 '19 at 8:18