I have no idea what is wrong with my program; after 1 hour of trying to fix it I have given up.
if finalp1score < finalp2score:
print(player2,"wins the game, with a total score of",player2bonus + player2score,"!")
if finalp1score > finalp2score:
print(player1,"wins the game, with a total score of",player1bonus + player1score,"!")
with open('leaderboard.csv', 'a', newline='') as file:
writer = csv.writer(file)
if finalp1score > finalp2score:
writer.writerow((finalp1score, player1))
file.close()
if finalp1score < finalp2score:
writer.writerow((finalp2score, player2))
file.close()
def getData(filename):
hiscores = []
file = open(filename, "r")
for line in file:
user = []
curLine = line.split(",")
user.append(curLine[0])
user.append(curLine[1])
hiscores.append(user)
sorted(hiscores)
return hiscores
def sortNum(data):
return sorted(data, reverse=True)
leaderboard = getData("leaderboard.csv")
sorted_lb1 = sortNum(leaderboard)
print("_______________________________________________")
print("Leaderboard:")
print("_______________________________________________")
MAX = 0
if len(sorted_lb1) > 10:
MAX = 10
else:
MAX = len(sorted_lb1)
for x in range(1,MAX):
print(x,".", sorted_lb1[x][1],"Score: " , sorted_lb1[x][0])
and when the the leaderboard.csv file is empty it returns with:
Traceback (most recent call last):
File "N:\GCSE NEA\2-player dice game.py", line 364, in <module>
leaderboard = getData("leaderboard.csv")
File "N:\GCSE NEA\2-player dice game.py", line 352, in getData
user.append(curLine[1])
IndexError: list index out of range
I want the code to still work when the leaderboard.csv file is empty.
Comments
Post a Comment