2 player dice game, where even total gains points and odd total loses points
up vote
1
down vote
favorite
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")
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
if ens == "n":
username=input("Please enter appropiate username: ")
password1=input("Please enter password: ")
password2=input("Please re-enter password: ")
if password1 == password2: # checking if both passwords entered are the same
print("your account has been successfully been made Thankyou")
file = open("accountfile.txt","a")
file.write("username: ")
file.write(username)
file.write(" ")
file.write("password: ")
file.write(password2)
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
correctPassword=(password1)
while True:
password=input('Enter password again ')
if password == correctPassword:
print('Correct password has been entered')
f = open ("accountfile.txt","a+")
f.write("username: ")
f.write(username)
f.write(" ")
f.write("password: ")
f.write(correctPassword)
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(" ")
print('Incorrect password ')
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
counter = 0
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:
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(username1)
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(username2)
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(username1)
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(username2)
file.write(" has ")
file.write(str(total_score2))
file.write(" points")
file.write("n")
file.close()
sys.exit()
else:
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()
else:
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()
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.
python homework dice
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
1
down vote
favorite
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")
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
if ens == "n":
username=input("Please enter appropiate username: ")
password1=input("Please enter password: ")
password2=input("Please re-enter password: ")
if password1 == password2: # checking if both passwords entered are the same
print("your account has been successfully been made Thankyou")
file = open("accountfile.txt","a")
file.write("username: ")
file.write(username)
file.write(" ")
file.write("password: ")
file.write(password2)
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
correctPassword=(password1)
while True:
password=input('Enter password again ')
if password == correctPassword:
print('Correct password has been entered')
f = open ("accountfile.txt","a+")
f.write("username: ")
f.write(username)
f.write(" ")
f.write("password: ")
f.write(correctPassword)
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(" ")
print('Incorrect password ')
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
counter = 0
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:
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(username1)
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(username2)
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(username1)
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(username2)
file.write(" has ")
file.write(str(total_score2))
file.write(" points")
file.write("n")
file.close()
sys.exit()
else:
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()
else:
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()
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.
python homework dice
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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")
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
if ens == "n":
username=input("Please enter appropiate username: ")
password1=input("Please enter password: ")
password2=input("Please re-enter password: ")
if password1 == password2: # checking if both passwords entered are the same
print("your account has been successfully been made Thankyou")
file = open("accountfile.txt","a")
file.write("username: ")
file.write(username)
file.write(" ")
file.write("password: ")
file.write(password2)
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
correctPassword=(password1)
while True:
password=input('Enter password again ')
if password == correctPassword:
print('Correct password has been entered')
f = open ("accountfile.txt","a+")
f.write("username: ")
f.write(username)
f.write(" ")
f.write("password: ")
f.write(correctPassword)
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(" ")
print('Incorrect password ')
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
counter = 0
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:
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(username1)
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(username2)
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(username1)
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(username2)
file.write(" has ")
file.write(str(total_score2))
file.write(" points")
file.write("n")
file.close()
sys.exit()
else:
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()
else:
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()
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.
python homework dice
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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")
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
if ens == "n":
username=input("Please enter appropiate username: ")
password1=input("Please enter password: ")
password2=input("Please re-enter password: ")
if password1 == password2: # checking if both passwords entered are the same
print("your account has been successfully been made Thankyou")
file = open("accountfile.txt","a")
file.write("username: ")
file.write(username)
file.write(" ")
file.write("password: ")
file.write(password2)
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
correctPassword=(password1)
while True:
password=input('Enter password again ')
if password == correctPassword:
print('Correct password has been entered')
f = open ("accountfile.txt","a+")
f.write("username: ")
f.write(username)
f.write(" ")
f.write("password: ")
f.write(correctPassword)
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(" ")
print('Incorrect password ')
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
counter = 0
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:
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(username1)
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(username2)
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(username1)
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(username2)
file.write(" has ")
file.write(str(total_score2))
file.write(" points")
file.write("n")
file.close()
sys.exit()
else:
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()
else:
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()
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.
python homework dice
python homework dice
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago
200_success
127k15148411
127k15148411
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 5 hours ago
colkat406
61
61
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
colkat406 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
- 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.
- Use
[unittests]2 to automatically test the functionality of those functions, 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, programmes 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, make it 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?
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
- 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.
- Use
[unittests]2 to automatically test the functionality of those functions, 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, programmes 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, make it 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?
add a comment |
up vote
2
down vote
- 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.
- Use
[unittests]2 to automatically test the functionality of those functions, 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, programmes 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, make it 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?
add a comment |
up vote
2
down vote
up vote
2
down vote
- 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.
- Use
[unittests]2 to automatically test the functionality of those functions, 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, programmes 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, make it 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?
- 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.
- Use
[unittests]2 to automatically test the functionality of those functions, 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, programmes 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, make it 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?
edited 7 mins ago
answered 3 hours ago
Martin Spamer
285212
285212
add a comment |
add a comment |
colkat406 is a new contributor. Be nice, and check out our Code of Conduct.
colkat406 is a new contributor. Be nice, and check out our Code of Conduct.
colkat406 is a new contributor. Be nice, and check out our Code of Conduct.
colkat406 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208342%2f2-player-dice-game-where-even-total-gains-points-and-odd-total-loses-points%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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