クラスのちょっとした雑学ゲームのハイスコア リストを作成しようとしています。ただし、辞書をランダムに印刷するだけでなく、印刷出力を取得できないように見えるため、問題が発生しています。このスニペットは、実際には完全なプログラムからのものではありません。これは、何かを台無しにしたくなかったため、機能を推論するための試みにすぎません。
scores = {'score1': {'initials': 'ywo',
'score': 20},
'score2': {'initials': 'JRV',
'score': 18},
'score3': {'initials': 'blh',
'score': 16},
'score4': {'initials': 'yth',
'score': 15},
'score5': {'initials': 'rtg',
'score': 12}}
total_score = 17
#iterates over the scores to see if new score and initials should be input
for i in (scores):
if total_score > scores[i]['score']:
scores[i]['initials'] = 'JKE'
scores[i]['score'] = total_score
break
#prints scores in a table like format rather than list
print("HIGH\tSCORES")
for i in scores:
print(scores[i]['initials'], "\t", scores[i]['score'])
私の出力は毎回ランダムです。辞書を最高から最低の順に印刷したいだけです。
ywo 20
JRV 18
JKE 17
などなど
私が抱えているもう 1 つの問題は、他のスコアを辞書の下位に移動する方法がわからないことです。したがって、JKE のスコアが blh のスコアを置き換える場合、blh は辞書から削除されるだけでなく、score4 インデックスに移動し、score4 値は score5 などに移動します。アドバイスをいただければ幸いです。ありがとうございました!