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. Also It would be very helpful to me if you could rewrite the parts of my code that need improving or are wrong step by step so I can understand it better
Thanks
Game for 2 computerized players that can input their names, throw 5 times 2 dices in turn, loose 5 points on odd and gain 10 point on even (lacking all the file stuff you do):
import random
def get_name(key):
n = ""
while not n:
n = input(f"Input your name, {key}: ")
return n
def throw_dice(key):
dices = random.choices(range(1,7),k=2)
print(f"{names[key]} threw: {dices}", end = " ")
if sum(dices) % 2 == 0:
score[key] += 10
print("Even. You earn 10 points.")
else:
score[key] -= 5
print("Odd. You loose 5 points")
def print_score():
for n in score:
print(f" {names[n]} has got {score[n]} points.")
def win_message(s,n):
print_score()
if score["P1"] > score["P2"]:
print(f"{names['P1']} won")
else:
print(f"{names['P2']} won")
player = ""
score = {}
names = {}
for n in ["P1","P2"]:
names[n] = get_name(n)
score[n] = 0
# twice the amount of rows wanted, because players take turn
for c in range(10):
# switches between player1 and player2
player = "P1" if player != "P1" else "P2"
print(f"Round {c//2 + 1}: it is {names[player]}'s turn.")
print_score()
throw_dice(player)
win_message(score,names)
Output:
Input your name, P1: Rabbit
Input your name, P2: Hare
Round 1: it is Rabbit's turn.
Rabbit has got 0 points.
Hare has got 0 points.
Rabbit threw: [6, 2] Even. You earn 10 points.
Round 1: it is Hare's turn.
Rabbit has got 10 points.
Hare has got 0 points.
Hare threw: [4, 2] Even. You earn 10 points.
Round 2: it is Rabbit's turn.
Rabbit has got 10 points.
Hare has got 10 points.
Rabbit threw: [6, 3] Odd. You loose 5 points
Round 2: it is Hare's turn.
Rabbit has got 5 points.
Hare has got 10 points.
Hare threw: [6, 5] Odd. You loose 5 points
Round 3: it is Rabbit's turn.
Rabbit has got 5 points.
Hare has got 5 points.
Rabbit threw: [4, 5] Odd. You loose 5 points
Round 3: it is Hare's turn.
Rabbit has got 0 points.
Hare has got 5 points.
Hare threw: [4, 2] Even. You earn 10 points.
Round 4: it is Rabbit's turn.
Rabbit has got 0 points.
Hare has got 15 points.
Rabbit threw: [3, 2] Odd. You loose 5 points
Round 4: it is Hare's turn.
Rabbit has got -5 points.
Hare has got 15 points.
Hare threw: [2, 3] Odd. You loose 5 points
Round 5: it is Rabbit's turn.
Rabbit has got -5 points.
Hare has got 10 points.
Rabbit threw: [4, 4] Even. You earn 10 points.
Round 5: it is Hare's turn.
Rabbit has got 5 points.
Hare has got 10 points.
Hare threw: [5, 3] Even. You earn 10 points.
Rabbit has got 5 points.
Hare has got 20 points.
Hare won