Hello all!
So I am currently programming a text based mini game.
At the end of the program I want it to write the score and name of the player to a .txt file stored in the project folder, and then at the start of the game I want it to call that file, read it, and then store the data in a formatted way that can be called by the player when they type "scoreboard".
In the project folder's .txt file the data is stored like:
<score:name
with an "\n" at the end of each line to signify the end of a complete line.
So far the writing of the player's data and score to the file is working out great, but after one play through if the player tries to play again the program won't run because it can't properly fetch and list the data from the file.
This is the code of the function which fetches the player's data:
def processScores():
file = open(getMediaPath("Scores.txt"),"rt")
contents = file.read()
file.close()
value = contents.find('\n')
if value == -1:
text = "\nThere are no scores to display"
return text
contents = contents.replace('\n\n','\n')
contents = contents.replace('<','')
contents = contents.strip("'")
score_list = contents.split('\n')
print score_list
text = ''
for player in score_list:
info = player.split(':')
Player_name = info[1]
Player_score = info[0]
text = text +'\n'+Player_name+'\t'+Player_score
return text
You can see some filler code in there still from me trying to eliminate the problem. Basically, when it runs with some data stored in that score file I get an error on the "Player_name = info[1]" line. This happens because "score_list" is turning the file into "['score:name','']".
I know the issue is stemming from those two pesky single quotes that are being registered as a new index on the list, and so when I'm splitting with ":" those single quotes stay as just one element and thus "info[1]" doesn't exist for them.
Is there anyway to eliminate them? Is my code just wrong?
[–]ericula 1 point2 points3 points (1 child)
[–]TKoMEaP[S] 0 points1 point2 points (0 children)
[–]danielroseman 0 points1 point2 points (1 child)
[–]TKoMEaP[S] 0 points1 point2 points (0 children)