4
\$\begingroup\$

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 :-)

asked Dec 2, 2018 at 20:04
\$\endgroup\$
1
  • \$\begingroup\$ Every contribution to this will help me alot so please make suggestions and as I said every contribution will be greatly appreciated \$\endgroup\$ Commented Dec 3, 2018 at 16:54

1 Answer 1

4
\$\begingroup\$

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.
answered Dec 3, 2018 at 2:56
\$\endgroup\$
5
  • \$\begingroup\$ 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.) \$\endgroup\$ Commented Dec 3, 2018 at 16:32
  • \$\begingroup\$ if I was to put the code you wrote into my code where would I put it ? \$\endgroup\$ Commented Dec 4, 2018 at 17:28
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 4, 2018 at 18:46
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ Commented Jul 5, 2019 at 8:18
  • \$\begingroup\$ @eric.m Why would all instances of Player share the same score. If you instantiate multiple players for a multi-player game, then they should have different score. Or maybe I misunderstand something. \$\endgroup\$ Commented Feb 27, 2020 at 17:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.