# 2 player dice game, where even total gains points and odd total loses points NEA task computer science

This is a game for two users who roll 2 dice 5 times. If the total of dice is even the player gains 10 points if it is odd, they lose 5.

If there it is a draw after five rounds then the both users will have to roll one die to determine the winner.

from random import randint
from time import sleep
import time
import sys
import random
import operator

total_score2 = 0
total_score1 = 0
rounds = 0
playerOnePoints = 0
playerTwoPoints = 0
print("*****************Welcome To The DICE Game*******************")
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
ens=input("")
while ens != ("e") and ens != ("n") and ens != ("s"): # if anything else but these characters are entered it will loop until it is correct
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user and enter 's' to display scores")
ens = input()
if ens == "s":
s = open("scores.txt","r")

users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}
best_player = max(users_points.items(), key=operator.itemgetter(1))[0]
print("\n")
print('player with maximum points is {}, this player has {} points'.format(best_player, users_points[best_player]))
best_players = sorted(users_points, key=users_points.get, reverse=True)
for bp in best_players:
print('{} has {} points'.format(bp, users_points[bp])) # This prints all players scores

if ens == "n":
file = open("accountfile.txt","a")
file.write(" ")
file.write("\n")
file.close()
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user")
ens=input(" ")
if password1 != password2: # if passwords entered are not the same will loop until they are correctly entered
while True:
f = open ("accountfile.txt","a+")
f.write(" ")
f.write("\n")
f.close()
print("Please enter 'n' if you are a new user and 'e' if you are a exsiting user")
en=input(" ")

if ens == "e":
counter = 0
check_failed = True
while check_failed:
print("you are logged in")
check_failed = False
counter = 0
check_failed = True
while check_failed:
print("you are logged in")
check_failed = False
time.sleep(1)
print("Welcome to the dice game")
time.sleep(1)
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 <= 0:
playerOnePoints = 0
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")
if playerOnePoints <= 0:
playerOnePoints = 0
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("-------------------------------------------")

if total_score1 > total_score2:
print("Player 1 Wins!")
file = open("scores.txt","a")
file.write(" has ")
file.write(str(total_score1))
file.write(" points")
file.write("\n")
file.close()
sys.exit()
if total_score2 > total_score1:
print("Player 2 Wins!")
file = open("scores.txt","a")
file.write(" has ")
file.write(str(total_score2))
file.write(" points")
file.write("\n")
file.close()
sys.exit()
if total_score1 == total_score2:
print("Its a draw!")
print("So both players will have to roll one more dice")
time.sleep(2)
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 % 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("-------------------------------------------")
if total_score1 > total_score2:
print("Player 1 Wins!")
file = open("scores.txt","a")
file.write(" has ")
file.write(str(total_score1))
file.write(" points")
file.write("\n")
file.close()
if total_score2 > total_score1:
print("Player 2 Wins!")
file = open("scores.txt","a")
file.write(" has ")
file.write(str(total_score2))
file.write(" points")
file.write("\n")
file.close()
sys.exit()
else:
counter = counter + 1
if counter == 3:
print("----------------------------------------------------")
print("You have been locked out please restart to try again")
sys.exit()

else:
counter = counter + 1
if counter == 3:
print("----------------------------------------------------")
print("You have been locked out please restart to try again")
sys.exit()


This was a project that i have been doing in computer science which I have now finished if anyone has any suggestions on how I could make it better they will appreciated alot so please suggest how I can improve it. Also It would be very helpful to me if you could rewrite the parts of my code that need improving or are wrong step by step so I can understand it better

Thanks

• Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. Feel free to post a follow-up question linking back to this one. – Mast Dec 22 '18 at 16:44
• I didn't know that I shouldn't do that sorry – colkat406 Dec 23 '18 at 10:44
• @Mast so I should post my updated code on a new question – colkat406 Dec 23 '18 at 10:48
• Yes. Make sure you add a piece of text about what it does, what is improved and a link to your original question (this one). – Mast Dec 23 '18 at 12:00
• No problem. Feel free to ask if anything is unclear. – Mast Dec 23 '18 at 15:35

• You need to run your UI text through a spell checker. Example - exsiting
• Since each player has more than one attribute (total_score, points, password, etc.) each player should be represented by a class, or at least a namedtuple.
• You have a print followed by a blank input(""). Don't do this; just put the content of the print into the prompt argument of the input call.
• while ens != ("e") and ens != ("n") and ens != ("s") should be something like while ens not in ('e', 'n', 's'):
• Rather than bare open/close calls, you should use a proper with statement.
• Rather than using a half a dozen write calls, consider just issuing one write call with a multi-line (triple-quoted) string.
• Something like "username: " + username1 + " password: " + password is better done with a format call.

Other than that, you really need to strengthen your DRY (don't repeat yourself) skills. Writing a handful of functions for repeated code would be a good start.

• Is it possible for you to re write my code so it isn't so long. it would be very helpful if you did it. :) – colkat406 Nov 25 '18 at 19:22
• @colkat406 absolutely not. Then you wouldn't learn. Try it yourself and submit a new review. – Reinderien Nov 25 '18 at 20:17
• could you show me an example of a namedtuple – colkat406 Nov 27 '18 at 17:00
• could you also show me how to use a function for username and password becasue it is not working for me – colkat406 Nov 27 '18 at 17:10
• If it isn't working, that's a question for StackOverflow :) – Reinderien Nov 27 '18 at 17:46
• The requirements for this game are unclear. Code is always produced to implement a set of well defined and understood requirements, even if you are defining these yourself.
• The code is monolithic, break it down into functions, this is functional decomposition and one of the first skills a programmer needs to learn, regardless of language or programming paradigm.
• Use unittests to automatically test the functionality of those methods, learning to use unittest to engage in Test Driven development will increase your speed of learning at first and make you a far better developer in the long run.
• The code has a very linear flow, programs should provide flexible flows, break it down into three parts, the setup, the game and the results, wrap these with a menu, to setup, play, see score or exit. Once each part is complete return to the menu loop.
• What is the point of requiring usernames then referring to Players 1 & 2.
• It include lots of pointless functionality such usernames and password from plain text files, capture the names, but do away with the password, security by obscurity offers no security at all.
• It fails if you enter S when first starting, if the file does not exist use Exception Handing to catch this failure, create the blank file and continue. Always attempt to recover from error conditions when possible, make this second nature.
• Always validate user input before proceeding.
• Learn to use the built in language features and libraries, this example plays a similar game with much better usage of Python's built in capabilities. How to make this random number game better?