Two-player dice game for NEA task computer science (Updated)












2














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 is a draw after five rounds then both users will have to roll one die to determine the winner.



Some updates that I have done to this code include adding functions to it so that it reduces the size of the code, removing repeated code, acting upon the suggestions that were given to me on my old code, and trying to improve my DRY (don't repeat yourself) skills.



I just want suggestions on how I could improve this updated code.



import time
import sys
import random
import operator
total_score2 = 0
total_score1 = 0
rounds = 0
playerOnePoints = 0
playerTwoPoints = 0
counter = 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 not in ('e', 'n', '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")
file_content = s.read().splitlines()
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("LeaderBoard: ")
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
print("n")
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 == "n":
file = open("accountfile.txt","r+")
text = file.read().strip().split()
check = True
while check:
username=input("Please enter appropiate username: ") #Takes input of a username from user
if username == "": #if no value is entered for the username
continue
if username in text: #username in present in the text file
print("Username is taken please try another one")
else: #username is absent in the text file
print("Username has been accepted")
check = False
check = True
while check:
password1=input("Please enter password: ")
password2=input("Please re-enter password: ")
if password1 == password2:
if password2 in text:
print("Password has been taken please try another one")
else:
print("Username and Password have sucessfully been made Thankyou")
file.write("username: " + username + " " + "password: " + password2 + "n")
file.close()
check = False
else:
print("passwords do not match please try again")
file.close()

def write1():
print("Player 1 ",username1," Wins!")
file = open("scores.txt","a")
file.write(username1 + " has " + str(total_score1) + " points" + "n")
file.close()
sys.exit()
def write2():
print("Player 2 ",username2," Wins!")
file = open("scores.txt","a")
file.write(username2 + " has " + str(total_score2) + " points" + "n")
file.close()
sys.exit()
def validation():
global counter
print("Sorry, this username or password does not exist please try again")
counter = counter + 1
if counter == 3:
print("----------------------------------------------------")
print("You have been locked out please restart to try again")
sys.exit()
def game():
global total_score1
global total_score2
global rounds
global number
global number2
global playerOnePoints
global playerTwoPoints
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("-------------------------------------------")
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 % 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")

if ens == "e":
counter = 0
check_failed = True
while check_failed:
print("Could player 1 enter their username and password")
username1=input("Please enter your username ")
password=input("Please enter your password ")
with open("accountfile.txt","r") as username_finder:
for line in username_finder:
if ("username: " + username1 + " password: " + password) == line.strip():
print("you are logged in")
check_failed = False
check_failed = True
while check_failed:
print("Could player 2 enter their username and password")
username2=input("Please enter your username ")
password=input("Please enter your password ")
with open("accountfile.txt","r") as username_finder:
for line in username_finder:
if ("username: " + username2 + " password: " + password) == line.strip():
print("you are logged in")
check_failed = False
time.sleep(1)
print("Welcome to the dice game")
time.sleep(1)
while rounds < 5:
game()
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:
write1()
if total_score2 > total_score1:
write2()
if total_score1 == total_score2:
print("Its a draw!")
game()
if total_score1 > total_score2:
write1()
if total_score1 < total_score2:
write2()
else:
validation()

else:
validation()


This is the link to my old code










share|improve this question





























    2














    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 is a draw after five rounds then both users will have to roll one die to determine the winner.



    Some updates that I have done to this code include adding functions to it so that it reduces the size of the code, removing repeated code, acting upon the suggestions that were given to me on my old code, and trying to improve my DRY (don't repeat yourself) skills.



    I just want suggestions on how I could improve this updated code.



    import time
    import sys
    import random
    import operator
    total_score2 = 0
    total_score1 = 0
    rounds = 0
    playerOnePoints = 0
    playerTwoPoints = 0
    counter = 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 not in ('e', 'n', '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")
    file_content = s.read().splitlines()
    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("LeaderBoard: ")
    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
    print("n")
    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 == "n":
    file = open("accountfile.txt","r+")
    text = file.read().strip().split()
    check = True
    while check:
    username=input("Please enter appropiate username: ") #Takes input of a username from user
    if username == "": #if no value is entered for the username
    continue
    if username in text: #username in present in the text file
    print("Username is taken please try another one")
    else: #username is absent in the text file
    print("Username has been accepted")
    check = False
    check = True
    while check:
    password1=input("Please enter password: ")
    password2=input("Please re-enter password: ")
    if password1 == password2:
    if password2 in text:
    print("Password has been taken please try another one")
    else:
    print("Username and Password have sucessfully been made Thankyou")
    file.write("username: " + username + " " + "password: " + password2 + "n")
    file.close()
    check = False
    else:
    print("passwords do not match please try again")
    file.close()

    def write1():
    print("Player 1 ",username1," Wins!")
    file = open("scores.txt","a")
    file.write(username1 + " has " + str(total_score1) + " points" + "n")
    file.close()
    sys.exit()
    def write2():
    print("Player 2 ",username2," Wins!")
    file = open("scores.txt","a")
    file.write(username2 + " has " + str(total_score2) + " points" + "n")
    file.close()
    sys.exit()
    def validation():
    global counter
    print("Sorry, this username or password does not exist please try again")
    counter = counter + 1
    if counter == 3:
    print("----------------------------------------------------")
    print("You have been locked out please restart to try again")
    sys.exit()
    def game():
    global total_score1
    global total_score2
    global rounds
    global number
    global number2
    global playerOnePoints
    global playerTwoPoints
    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("-------------------------------------------")
    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 % 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")

    if ens == "e":
    counter = 0
    check_failed = True
    while check_failed:
    print("Could player 1 enter their username and password")
    username1=input("Please enter your username ")
    password=input("Please enter your password ")
    with open("accountfile.txt","r") as username_finder:
    for line in username_finder:
    if ("username: " + username1 + " password: " + password) == line.strip():
    print("you are logged in")
    check_failed = False
    check_failed = True
    while check_failed:
    print("Could player 2 enter their username and password")
    username2=input("Please enter your username ")
    password=input("Please enter your password ")
    with open("accountfile.txt","r") as username_finder:
    for line in username_finder:
    if ("username: " + username2 + " password: " + password) == line.strip():
    print("you are logged in")
    check_failed = False
    time.sleep(1)
    print("Welcome to the dice game")
    time.sleep(1)
    while rounds < 5:
    game()
    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:
    write1()
    if total_score2 > total_score1:
    write2()
    if total_score1 == total_score2:
    print("Its a draw!")
    game()
    if total_score1 > total_score2:
    write1()
    if total_score1 < total_score2:
    write2()
    else:
    validation()

    else:
    validation()


    This is the link to my old code










    share|improve this question



























      2












      2








      2


      2





      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 is a draw after five rounds then both users will have to roll one die to determine the winner.



      Some updates that I have done to this code include adding functions to it so that it reduces the size of the code, removing repeated code, acting upon the suggestions that were given to me on my old code, and trying to improve my DRY (don't repeat yourself) skills.



      I just want suggestions on how I could improve this updated code.



      import time
      import sys
      import random
      import operator
      total_score2 = 0
      total_score1 = 0
      rounds = 0
      playerOnePoints = 0
      playerTwoPoints = 0
      counter = 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 not in ('e', 'n', '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")
      file_content = s.read().splitlines()
      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("LeaderBoard: ")
      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
      print("n")
      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 == "n":
      file = open("accountfile.txt","r+")
      text = file.read().strip().split()
      check = True
      while check:
      username=input("Please enter appropiate username: ") #Takes input of a username from user
      if username == "": #if no value is entered for the username
      continue
      if username in text: #username in present in the text file
      print("Username is taken please try another one")
      else: #username is absent in the text file
      print("Username has been accepted")
      check = False
      check = True
      while check:
      password1=input("Please enter password: ")
      password2=input("Please re-enter password: ")
      if password1 == password2:
      if password2 in text:
      print("Password has been taken please try another one")
      else:
      print("Username and Password have sucessfully been made Thankyou")
      file.write("username: " + username + " " + "password: " + password2 + "n")
      file.close()
      check = False
      else:
      print("passwords do not match please try again")
      file.close()

      def write1():
      print("Player 1 ",username1," Wins!")
      file = open("scores.txt","a")
      file.write(username1 + " has " + str(total_score1) + " points" + "n")
      file.close()
      sys.exit()
      def write2():
      print("Player 2 ",username2," Wins!")
      file = open("scores.txt","a")
      file.write(username2 + " has " + str(total_score2) + " points" + "n")
      file.close()
      sys.exit()
      def validation():
      global counter
      print("Sorry, this username or password does not exist please try again")
      counter = counter + 1
      if counter == 3:
      print("----------------------------------------------------")
      print("You have been locked out please restart to try again")
      sys.exit()
      def game():
      global total_score1
      global total_score2
      global rounds
      global number
      global number2
      global playerOnePoints
      global playerTwoPoints
      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("-------------------------------------------")
      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 % 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")

      if ens == "e":
      counter = 0
      check_failed = True
      while check_failed:
      print("Could player 1 enter their username and password")
      username1=input("Please enter your username ")
      password=input("Please enter your password ")
      with open("accountfile.txt","r") as username_finder:
      for line in username_finder:
      if ("username: " + username1 + " password: " + password) == line.strip():
      print("you are logged in")
      check_failed = False
      check_failed = True
      while check_failed:
      print("Could player 2 enter their username and password")
      username2=input("Please enter your username ")
      password=input("Please enter your password ")
      with open("accountfile.txt","r") as username_finder:
      for line in username_finder:
      if ("username: " + username2 + " password: " + password) == line.strip():
      print("you are logged in")
      check_failed = False
      time.sleep(1)
      print("Welcome to the dice game")
      time.sleep(1)
      while rounds < 5:
      game()
      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:
      write1()
      if total_score2 > total_score1:
      write2()
      if total_score1 == total_score2:
      print("Its a draw!")
      game()
      if total_score1 > total_score2:
      write1()
      if total_score1 < total_score2:
      write2()
      else:
      validation()

      else:
      validation()


      This is the link to my old code










      share|improve this question















      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 is a draw after five rounds then both users will have to roll one die to determine the winner.



      Some updates that I have done to this code include adding functions to it so that it reduces the size of the code, removing repeated code, acting upon the suggestions that were given to me on my old code, and trying to improve my DRY (don't repeat yourself) skills.



      I just want suggestions on how I could improve this updated code.



      import time
      import sys
      import random
      import operator
      total_score2 = 0
      total_score1 = 0
      rounds = 0
      playerOnePoints = 0
      playerTwoPoints = 0
      counter = 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 not in ('e', 'n', '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")
      file_content = s.read().splitlines()
      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("LeaderBoard: ")
      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
      print("n")
      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 == "n":
      file = open("accountfile.txt","r+")
      text = file.read().strip().split()
      check = True
      while check:
      username=input("Please enter appropiate username: ") #Takes input of a username from user
      if username == "": #if no value is entered for the username
      continue
      if username in text: #username in present in the text file
      print("Username is taken please try another one")
      else: #username is absent in the text file
      print("Username has been accepted")
      check = False
      check = True
      while check:
      password1=input("Please enter password: ")
      password2=input("Please re-enter password: ")
      if password1 == password2:
      if password2 in text:
      print("Password has been taken please try another one")
      else:
      print("Username and Password have sucessfully been made Thankyou")
      file.write("username: " + username + " " + "password: " + password2 + "n")
      file.close()
      check = False
      else:
      print("passwords do not match please try again")
      file.close()

      def write1():
      print("Player 1 ",username1," Wins!")
      file = open("scores.txt","a")
      file.write(username1 + " has " + str(total_score1) + " points" + "n")
      file.close()
      sys.exit()
      def write2():
      print("Player 2 ",username2," Wins!")
      file = open("scores.txt","a")
      file.write(username2 + " has " + str(total_score2) + " points" + "n")
      file.close()
      sys.exit()
      def validation():
      global counter
      print("Sorry, this username or password does not exist please try again")
      counter = counter + 1
      if counter == 3:
      print("----------------------------------------------------")
      print("You have been locked out please restart to try again")
      sys.exit()
      def game():
      global total_score1
      global total_score2
      global rounds
      global number
      global number2
      global playerOnePoints
      global playerTwoPoints
      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("-------------------------------------------")
      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 % 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")

      if ens == "e":
      counter = 0
      check_failed = True
      while check_failed:
      print("Could player 1 enter their username and password")
      username1=input("Please enter your username ")
      password=input("Please enter your password ")
      with open("accountfile.txt","r") as username_finder:
      for line in username_finder:
      if ("username: " + username1 + " password: " + password) == line.strip():
      print("you are logged in")
      check_failed = False
      check_failed = True
      while check_failed:
      print("Could player 2 enter their username and password")
      username2=input("Please enter your username ")
      password=input("Please enter your password ")
      with open("accountfile.txt","r") as username_finder:
      for line in username_finder:
      if ("username: " + username2 + " password: " + password) == line.strip():
      print("you are logged in")
      check_failed = False
      time.sleep(1)
      print("Welcome to the dice game")
      time.sleep(1)
      while rounds < 5:
      game()
      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:
      write1()
      if total_score2 > total_score1:
      write2()
      if total_score1 == total_score2:
      print("Its a draw!")
      game()
      if total_score1 > total_score2:
      write1()
      if total_score1 < total_score2:
      write2()
      else:
      validation()

      else:
      validation()


      This is the link to my old code







      python python-3.x game homework dice






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 59 mins ago









      Reinderien

      2,992720




      2,992720










      asked 2 hours ago









      colkat406

      847




      847






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Try to avoid using so many globals. Your code would be better-structured if you made a Game class and captured most or all of that state as class member variables.



          You made the same spelling mistake here as you did in your previous question. "exsiting" is spelled "existing".



          In this code:



          s = open("scores.txt","r")


          You open, but fail to close, s. Convert this to a with statement.



          This:



          users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}


          relies on this format:



          file.write(username1 + " has " + str(total_score1) + " points" + "n")


          As such, you can convert your users_points initialization to:



          users_points = {}
          for line in file_content:
          user, points = re.match('r(w+) has (d+) points').groups()
          users_points[user] = int(points)


          However, that's not ideal. If scores.txt doesn't need to be human-readable, then you should store it in a different format - probably JSON. That way, your loading and store can be made much more simple.



          Move your global code to a main method.



          As I recommended in the previous incarnation of this question, and will recommend again, stop issuing blank input calls. This:



          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("")


          needs to be



          ens = input("Please enter 'n' if you are a new user, 'e' if you are an existing user, or 's' to display scores: ")


          Try to convert some of your concatenated strings into f-strings:



          username1 + " has " + str(total_score1) + " points" + "n")


          should become



          f'{username1} has {total_score1} pointsn'


          This:



          counter = counter + 1


          should be



          counter += 1


          This:



          check_failed = False
          check_failed = True


          is quite strange; the first assignment will be overwritten so you should probably just delete it.






          share|improve this answer























            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210517%2ftwo-player-dice-game-for-nea-task-computer-science-updated%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Try to avoid using so many globals. Your code would be better-structured if you made a Game class and captured most or all of that state as class member variables.



            You made the same spelling mistake here as you did in your previous question. "exsiting" is spelled "existing".



            In this code:



            s = open("scores.txt","r")


            You open, but fail to close, s. Convert this to a with statement.



            This:



            users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}


            relies on this format:



            file.write(username1 + " has " + str(total_score1) + " points" + "n")


            As such, you can convert your users_points initialization to:



            users_points = {}
            for line in file_content:
            user, points = re.match('r(w+) has (d+) points').groups()
            users_points[user] = int(points)


            However, that's not ideal. If scores.txt doesn't need to be human-readable, then you should store it in a different format - probably JSON. That way, your loading and store can be made much more simple.



            Move your global code to a main method.



            As I recommended in the previous incarnation of this question, and will recommend again, stop issuing blank input calls. This:



            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("")


            needs to be



            ens = input("Please enter 'n' if you are a new user, 'e' if you are an existing user, or 's' to display scores: ")


            Try to convert some of your concatenated strings into f-strings:



            username1 + " has " + str(total_score1) + " points" + "n")


            should become



            f'{username1} has {total_score1} pointsn'


            This:



            counter = counter + 1


            should be



            counter += 1


            This:



            check_failed = False
            check_failed = True


            is quite strange; the first assignment will be overwritten so you should probably just delete it.






            share|improve this answer




























              0














              Try to avoid using so many globals. Your code would be better-structured if you made a Game class and captured most or all of that state as class member variables.



              You made the same spelling mistake here as you did in your previous question. "exsiting" is spelled "existing".



              In this code:



              s = open("scores.txt","r")


              You open, but fail to close, s. Convert this to a with statement.



              This:



              users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}


              relies on this format:



              file.write(username1 + " has " + str(total_score1) + " points" + "n")


              As such, you can convert your users_points initialization to:



              users_points = {}
              for line in file_content:
              user, points = re.match('r(w+) has (d+) points').groups()
              users_points[user] = int(points)


              However, that's not ideal. If scores.txt doesn't need to be human-readable, then you should store it in a different format - probably JSON. That way, your loading and store can be made much more simple.



              Move your global code to a main method.



              As I recommended in the previous incarnation of this question, and will recommend again, stop issuing blank input calls. This:



              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("")


              needs to be



              ens = input("Please enter 'n' if you are a new user, 'e' if you are an existing user, or 's' to display scores: ")


              Try to convert some of your concatenated strings into f-strings:



              username1 + " has " + str(total_score1) + " points" + "n")


              should become



              f'{username1} has {total_score1} pointsn'


              This:



              counter = counter + 1


              should be



              counter += 1


              This:



              check_failed = False
              check_failed = True


              is quite strange; the first assignment will be overwritten so you should probably just delete it.






              share|improve this answer


























                0












                0








                0






                Try to avoid using so many globals. Your code would be better-structured if you made a Game class and captured most or all of that state as class member variables.



                You made the same spelling mistake here as you did in your previous question. "exsiting" is spelled "existing".



                In this code:



                s = open("scores.txt","r")


                You open, but fail to close, s. Convert this to a with statement.



                This:



                users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}


                relies on this format:



                file.write(username1 + " has " + str(total_score1) + " points" + "n")


                As such, you can convert your users_points initialization to:



                users_points = {}
                for line in file_content:
                user, points = re.match('r(w+) has (d+) points').groups()
                users_points[user] = int(points)


                However, that's not ideal. If scores.txt doesn't need to be human-readable, then you should store it in a different format - probably JSON. That way, your loading and store can be made much more simple.



                Move your global code to a main method.



                As I recommended in the previous incarnation of this question, and will recommend again, stop issuing blank input calls. This:



                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("")


                needs to be



                ens = input("Please enter 'n' if you are a new user, 'e' if you are an existing user, or 's' to display scores: ")


                Try to convert some of your concatenated strings into f-strings:



                username1 + " has " + str(total_score1) + " points" + "n")


                should become



                f'{username1} has {total_score1} pointsn'


                This:



                counter = counter + 1


                should be



                counter += 1


                This:



                check_failed = False
                check_failed = True


                is quite strange; the first assignment will be overwritten so you should probably just delete it.






                share|improve this answer














                Try to avoid using so many globals. Your code would be better-structured if you made a Game class and captured most or all of that state as class member variables.



                You made the same spelling mistake here as you did in your previous question. "exsiting" is spelled "existing".



                In this code:



                s = open("scores.txt","r")


                You open, but fail to close, s. Convert this to a with statement.



                This:



                users_points = {i.split()[0]: int(i.split()[2]) for i in file_content}


                relies on this format:



                file.write(username1 + " has " + str(total_score1) + " points" + "n")


                As such, you can convert your users_points initialization to:



                users_points = {}
                for line in file_content:
                user, points = re.match('r(w+) has (d+) points').groups()
                users_points[user] = int(points)


                However, that's not ideal. If scores.txt doesn't need to be human-readable, then you should store it in a different format - probably JSON. That way, your loading and store can be made much more simple.



                Move your global code to a main method.



                As I recommended in the previous incarnation of this question, and will recommend again, stop issuing blank input calls. This:



                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("")


                needs to be



                ens = input("Please enter 'n' if you are a new user, 'e' if you are an existing user, or 's' to display scores: ")


                Try to convert some of your concatenated strings into f-strings:



                username1 + " has " + str(total_score1) + " points" + "n")


                should become



                f'{username1} has {total_score1} pointsn'


                This:



                counter = counter + 1


                should be



                counter += 1


                This:



                check_failed = False
                check_failed = True


                is quite strange; the first assignment will be overwritten so you should probably just delete it.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 9 mins ago

























                answered 42 mins ago









                Reinderien

                2,992720




                2,992720






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210517%2ftwo-player-dice-game-for-nea-task-computer-science-updated%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Morgemoulin

                    Scott Moir

                    Souastre