0

How can I create a list that I can add to at the end of the piece of code and that I can use and add more to the next time I run through the code?

So I’m doing my NEA for GCSE Computer Science and I need to creat this game and save the top 5 scores in an external file. My plan is to put all of scores from game into a list, then sort the list so that the scores are in descending order, then display the first 5 elements of the list in the external file. I can’t create the list before I enter the score because it will be empty when I run the code again and I can’t add anything to a list that I haven’t made yet!

    Top5=[]    
    Top5.append([name, score])
    #I can’t use this because it will wipe the list every time I use the code


    Top5.append([name, score])
    #I can’t use this because there is no list created to add to

Basically, I want a list that I can keep adding things to every time I run through the code. How do I do this?

  • You should save the data to a JSON and if this is being run in a browser, you could use cookies – Sean Jan 12 '19 at 15:44
  • A file is fine if just doing this as an exercise, but then you need a little bit of code to read the data from the file at the start of the program. – Robin Zigmond Jan 12 '19 at 15:46

3 Answers3

0

As you have to list the top scorers in an external file, So simply create an external text file and read/write from that file

Obaid Ur Rehman
  • 324
  • 2
  • 15
  • The file should display the top 5 scores ever played in the game, so my plan was to create a list so I could put the scores in descending order and print the first 5 elements in the file. But I need to be able to create the file and always be able to add more scores to it. How can I do this? – Anna Tabner Jan 12 '19 at 16:01
  • I would love to help but as it is your project so it won't be right. Take a look at python file handling and you could easily understand this. Your logic of putting data in descending order and printing top 5 is not right as it will increase the size of file every time you run it even when there is no new top scorer. Simply compare the results of program with the text file using file handling and save the results. Here is the link for help : https://www.guru99.com/reading-and-writing-files-in-python.html – Obaid Ur Rehman Jan 12 '19 at 16:16
0

Save the scores in a file, and read the file when you start up. If the file doesn't exist yet, use an empty list.

import json

try:
    with open("highscores.json") as f:
        top5 = json.load(f)
except:
    top5 = []

# play game

if score > top5[-1]['score']:
    # Add new score to high scores
    top5.append({"player": name, "score": score})
    top5 = sorted(top5, key = lambda e: e['score'], reverse = True)[:5]

with open("highscores.json", "w") as f:
    json.dump(top5, f)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

you need to save the data in a file somewhere. You can use pickle as stated in this answer:

import pickle

name = 'name'
score = 'score'
top5 = []
top5.append([name, score])

with open('filename.pickle', 'wb') as handle:
    pickle.dump(top5, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)
    print(b) # b is your original object top5
Marco Maia
  • 25
  • 1
  • 4