-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling.py
44 lines (35 loc) · 1.12 KB
/
file_handling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import json
import datetime
filename = "savefile.json"
def retrieve_high_scores():
"""Return a dict of names and the highscores. """
high_scores = []
try:
with open(filename, 'r', encoding="utf-8") as f:
datas = json.load(f)
for data in datas:
high_scores.append(data)
except:
print("no highscores saved yet")
return None
high_scores.sort(key=lambda x: x['score'], reverse=True)
return high_scores
def save_score(name, score):
"""Save a high score to file. """
high_scores = []
new_score = dict(name=name, score=score, time=str(datetime.datetime.now(tz=None)))
if retrieve_high_scores() == None:
high_scores.append(new_score)
with open(filename, 'w+', encoding="utf-8") as f:
json.dump(high_scores, f)
return
for entry in retrieve_high_scores():
high_scores.append(entry)
high_scores.append(new_score)
with open(filename, 'w+', encoding="utf-8") as f:
json.dump(high_scores, f)
def main():
save_score("Taliban", 70)
if __name__ == "__main__":
main()