15 — High score: a tiny project
Everything you know fits into one small project: a dice game that remembers
its best score. use random rolls the die, use file saves the record.
Steps
Create
best.nmeand save the whole game as one file:
run it →use random latest use file latest import os ask name What is your name? score = 0 3 times: roll = random_number(1, 6) if roll >= 4: score add 1 say f"{name} rolled {roll} (score {score})" if os.path.exists("best.json"): best = json_load("best.json") say f"Last best was {best['score']}" else: best = {"name": "nobody", "score": 0} if score > best["score"]: json_save("best.json", {"name": name, "score": score}) say f"New best: {score}" else: say f"Best stays {best['score']}"Three rounds roll a die; each roll of 4 or more adds 1 to the score. The
iflines read a saved best and save a new best when the score beats it.Run it and type a name. One run might print:
nme run bestWhat is your name? Mina rolled 6 (score 1) Mina rolled 6 (score 2) Mina rolled 5 (score 3) New best: 3Your rolls and score will differ — the dice are random. The first run has no saved best, so the score always becomes the new best.
Run it again: the previous best comes back from the file before the game starts, and
best.jsonholds{"name": "Mina", "score": 3}. Beating it saves a new best; otherwise the old one stays.Korean writes the same project with
랜덤 사용 최신,파일 사용 최신,점수에 1 더해, andjson저장. The full Korean program is in the Korean guide; this snippet loads a saved best:
run it →파일 사용 최신 최고 = json읽기("best.json") 말해 f"저장된 최고 점수: {최고['score']}점"
Try it yourself
Make the game harder: change 3 times: to 4 times: for a longer game, or
change roll >= 4 to roll >= 5 for a stricter score.
What you learned
- One project can combine
use random,use file,ask,3 times:,if, and JSON in a single file. os.path.exists(path)checks whether a saved file is there yet.json_loadrestores the previous best;json_savewrites a new one.score add 1/점수에 1 더해grows the score only inside the condition block.