so a friend and I are working on a game in python. However, when we get the code to write out to the leaderboard, stored as an external file of the csv filetype, it always seems to add a blank line inbetween inputs.
the code is as follows:
def leaderBoardWrite(winnerName,winnerScore):
with open('leaderboard.csv', mode='a') as leaderboard:
winnerScoreSort = "{:03d}".format(winnerScore)
print(winnerScoreSort)
leaderboardWrite = csv.writer(leaderboard, delimiter=',')
leaderboardWrite.writerow([winnerScoreSort,winnerName]) ## This writes over the leaderboard, adding the newest score into it.
leaderBoardRead()
any advice?
thanks,
EDIT: since some people want a broader spec of the code, I will quickly forward it all so you have a full method of reproducing the code
## This is the start of the Dice game, look below at all of the comments to see what I have done once I have coded it.
## The next lines of code import all the nessasory modules for the program to run.
## The CSV Module is for the scoreboard
## Time is for the delays
## Random is for the
## Os is to clear the shell once the program has runself.
import csv,time,random,os
Complete = False ## Sets complete to false because the game has not begun
rounds = 0 ## Sets the rounds to 0 becasue game has not begun
playerOneScore = 000 ## Sets the player one score to 0 because they have not stated scoring yet
playerTwoScore = 000 ## Sets the player two score to 0 because they have not stated scoring yet
dice = [1,2,3,4,5,6] ## All the possible dice numbers
print("""
__ __ _ _ _ _
\ \ / / | | | | | | | |
\ \ /\ / /__| | ___ ___ _ __ ___ ___ | | | |___ ___ _ __| |
\ \/ \/ / _ \ |/ __/ _ \| '_ ` _ \ / _ \ | | | / __|/ _ \ '__| |
\ /\ / __/ | (_| (_) | | | | | | __/ | |__| \__ \ __/ | |_|
\/ \/ \___|_|\___\___/|_| |_| |_|\___| \____/|___/\___|_| (_) V.1.0.2
""")
time.sleep(1) ##Delay between welcome user and rules
print ("Welcome to my dice game! It is very simple to start playing.\nInput your username and password that were given to you and then type yes to verify you want to play (Please note 2 users both need to do this)\nBoth players will roll the dice twice\nThe totals of the rolls will be added to your net score\nIf your total is ODD, you will lose 5 points off of your overall score\nIf your total is EVEN, you will gain 10 points!\nIf you roll a double, the game will re-roll for you and you will gain the number of points rolled added to your score.\nThe winner is the person who has the most points at the end of the 5 rounds, once you have finished playing, you can see a leaderboard of players who have scored the most points.\nHave fun and good luck!")##Rules will be shown once at the start, straight after rules it will show the time delay.
print ("") ## Space between rules and dice delay
time.sleep(1) ## Delay between rules and dice delay
def PlayerOne(userOneName,playerOneScore,dice,delay): ## The function for Player One to start rolling and scoring
input("[{}] When you are ready, press the ENTER key to roll the dice!".format(userOneName))
diceOneRoll = dice[random.randint(0,len(dice)-1)] ## Rolls the first dice
diceTwoRoll = dice[random.randint(0,len(dice)-1)] ## Rols the seccond dice
total = diceOneRoll + diceTwoRoll ## Adds the scores from both dice rolls together
if diceOneRoll == diceTwoRoll: ## Checks if both os the dice rolls were the same
print("[{}] You have rolled a double, you get another roll!".format(userOneName))
temp = PlayerOneReRoll(userOneName,dice) ## Calls a function to roll an extra die
print("[{}] You scored {} from the reroll".format(userOneName,temp))
playerOneScoreReturn = total + temp ## Adds the score of the reroll to their total score
elif total % 2 == 0: ## Checks if the total of the two dice is even
print("[{}] Your total was even, take an extra 10 points".format(userOneName))
playerOneScoreReturn = total + 10
pass
elif total % 2 ==1: ## Checks if the total of the two dice is odd
print("[{}] You have scored an odd number, you lose 5 points".format(userOneName))
if playerOneScore-5 <0:
playerOneScoreReturn = 0
else:
playerOneScoreReturn = -5
else:
pass
time.sleep(delay)
print("[{}] You scored {}".format(userOneName,total))
time.sleep(delay)
print("[{}] Your total score is {}".format(userOneName,playerOneScore+playerOneScoreReturn))
return playerOneScoreReturn
def PlayerTwo(userTwoName,playerTwoScore,dice,delay):
input("\n[{}] When you are ready, press the ENTER key to roll the dice!".format(userTwoName))
diceOneRoll = dice[random.randint(0,len(dice)-1)]
diceTwoRoll = dice[random.randint(0,len(dice)-1)]
total = diceOneRoll + diceTwoRoll
if diceOneRoll == diceTwoRoll:
print("[{}] You have rolled a double, you get another roll!".format(userTwoName))
temp = PlayerTwoReRoll(userTwoName,dice)
print("[{}] You scored {} from the reroll".format(userTwoName,temp))
playerTwoScoreReturn = total + temp
elif total % 2 == 0:
print("[{}] Your total was even, take an extra 10 points".format(userTwoName))
playerTwoScoreReturn = total + 10
pass
elif total % 2 ==1:
print("[{}] You have scored an odd number, you lose 5 points".format(userTwoName))
if playerTwoScore-5 <0:
playerTwoScoreReturn = 0
else:
playerTwoScoreReturn = -5
else:
pass
time.sleep(delay)
print("[{}] You scored {}".format(userTwoName,total))
time.sleep(delay)
print("[{}] Your total score is {}".format(userTwoName,playerTwoScore+playerTwoScoreReturn))
return playerTwoScoreReturn
def PlayerOneReRoll(userOneName,dice):
input("[{}] Ready to roll the dice again? Press ENTER key to roll.".format(userOneName))
diceOneRoll = dice[random.randint(0,len(dice)-1)]
total = diceOneRoll
return total
def PlayerTwoReRoll(userTwoName,dice):
input("[{}] ".format(userTwoName))
diceOneRoll = dice[random.randint(0,len(dice)-1)]
total = diceOneRoll
return total
def main(userOneName,userTwoName,rounds,playerOneScore,playerTwoScore,Complete):
print("\nRound [{}/5]\n".format(rounds))
playerOneScoreTemp = PlayerOne(userOneName,playerOneScore,dice,delay)
playerTwoScoreTemp = PlayerTwo(userTwoName,playerTwoScore,dice,delay)
return playerOneScoreTemp,playerTwoScoreTemp
def draw(userOneName,userTwoName,dice,Complete):
print("draw")
playerOneScoreTemp = PlayerOneReRoll(userOneName,dice)
playerTwoScoreTemp = PlayerTwoReRoll(userTwoName,dice)
print("p1 = ",playerOneScoreTemp,"p2 = ",playerTwoScoreTemp)
checkForWinner(userOneName,userTwoName,Complete,playerOneScoreTemp,playerTwoScoreTemp)
def checkForWinner(userOneName,userTwoName,Complete,playerOneScore,playerTwoScore):
if Complete == True:
if playerOneScore > playerTwoScore:
print("\n[{}] You win with a score of {}. Well done".format(userOneName,playerOneScore))
tmp = playerOneScore
leaderBoardWrite(userOneName,tmp)
elif playerTwoScore > playerOneScore:
print("\n[{}] You win with a score of {}. Well done".format(userTwoName,playerTwoScore))
tmp = playerTwoScore
leaderBoardWrite(userTwoName,tmp)
elif playerOneScore == playerTwoScore:
print("\n[Both] It's come to a draw, play one last round to see who the ultimate champion is!" )
draw(userOneName,userTwoName,dice,Complete)
else:
pass
else:
pass
def leaderBoardWrite(winnerName,winnerScore):
with open('leaderboard.csv', mode='a') as leaderboard:
winnerScoreSort = "{:03d}".format(winnerScore)
leaderboardWrite = csv.writer(leaderboard, delimiter=',')
leaderboardWrite.writerow([winnerScoreSort,winnerName]) ## This writes over the leaderboard, adding the newest score into it.
leaderBoardRead()
def leaderBoardRead():## Shows the ASCII text of "Leaderboard"
print ("""
_ ______ _____ ______ _____ ____ ____ _____ _____
| | | ____| /\ | __ \| ____| __ \| _ \ / __ \ /\ | __ \| __ \
| | | |__ / \ | | | | |__ | |__) | |_) | | | | / \ | |__) | | | |
| | | __| / /\ \ | | | | __| | _ /| _ <| | | |/ /\ \ | _ /| | | |
| |____| |____ / ____ \| |__| | |____| | \ \| |_) | |__| / ____ \| | \ \| |__| |
|______|______/_/ \_\_____/|______|_| \_\____/ \____/_/ \_\_| \_\_____/
""")
data = open('leaderboard.csv')
for row in sorted(csv.reader(data)):
print(row) ## This reads the leaderboard and allows it to be shown in the code
delay = int(input("Please input a time delay for the dice to roll?\n: "))
## User One Authorisation
with open("logins.csv", newline= '') as csvnames:
authentication = csv.reader(csvnames, delimiter=",")
logins = list(authentication)
while True:
print("Welcome user one!")
userNameInput = input("Please input your username? \n: ")
userPasswordInput = input("Please input your password? \n: ")
for i in range(len(logins)):
if userNameInput == logins[i][0] and userPasswordInput == logins[i][1]:
userOneName = logins[i][0]
userOneAuthorised = True
break
else:
userOneAuthorised = False
if userOneAuthorised == False:
print("Sorry {}, you aren't authorised so run this task! \nPlease contact your System Administrator".format(userNameInput))
else:
try:
ready = input("Welcome {}, you have been Authorised. Are you ready to play? (Please type **yes** or **no**) \n:_ ".format(userOneName))
if ready[0].lower() == "y":
userOneAuthorised = True
break
else:
print("Please come back later, the game will be waiting for you!")
except:
pass
## User Two Authorisation
while True:
print("Welcome user 2!")
userNameInput = input("What is your username? \n: ")
userPasswordInput = input("What is your password? \n: ")
for i in range(len(logins)):
if userNameInput == logins[i][0] and userPasswordInput == logins[i][1]:
userTwoName = logins[i][0]
userTwoAuthorised = True
break
else:
userTwoAuthorised = False
if userTwoAuthorised == False:
print("Sorry {}, you aren't authorised so run this task! \nPlease contact your System Administrator".format(userNameInput))
else:
ready = input("Welcome {}, you have been Authorised. Are you ready to play? (Please type **yes** or **no**) \n:_ ".format(userTwoName))
try:
if ready[0].lower() == "y":
userTwoAuthorised = True
break
else:
print("Please come back later, the game will be waiting for you!")
except:
pass
if userOneAuthorised == True and userTwoAuthorised == True:
print("Starting")
for i in range(1,6):
roundNums = main(userOneName,userTwoName,i,playerOneScore,playerTwoScore,Complete)
playerOneScore+=roundNums[0]
playerTwoScore+=roundNums[1]
if i == 5:
Complete = True
else:
pass
checkForWinner(userOneName,userTwoName,Complete,playerOneScore,playerTwoScore)
else:
pass
time.sleep(4)
sorry it's in such an awkward order, my friend seems to like ordering things funny. thanks for all advice so far.