the task was to read a text file and input the data in to an array. Sounds simple enough. The text file stores the persons name and the score each with a "\n" character at the end so it moves to the next line. I wrote a program to add both of these to a temp array and then write that array in to the main array using the index values. problem is each of the values instead of getting written in to the given index writes it self to all the indexes therefor, over writing the previously written values.
I tried dry running the program but when i do that according to my logic it works fine by the output says otherwise. I was expecting an array at the end containing each of the players names and scores. The scores and the text file are attached bellow
Code :
def ReadHighScore():
Read_File = open("HighScore.txt","r")
Count = 0
Temp = ["",""]
index = 0
for Data in Read_File:
print ("First Count",Count)
Count += 1
print ("Last Count",Count)
Data = Data.strip("\n")
if Count % 2 != 0:
Temp[0] = Data
if Count % 2 == 0:
Temp[1] = Data
Player_Array[index] = Temp
index += 1
print (Player_Array)
Read_File.close()
Player_Array = ["" for row in range (11)]
ReadHighScore()
Text File:
FYI
10000
ABC
9092
KEL
8500
PAI
8203
BBB
7980
ACE
7246
GKL
7001
JSI
6490
EIF
6003
DIS
2000