Files
Python/python代码/深渊轮数统计.py
T

49 lines
2.1 KiB
Python

#深渊轮数统计
def read_lunshu(scores_game_name):
player_scores = {}
try:
test = open('深渊轮数.txt','r',encoding="utf_8")
next(test)
for line in test:
name,hundun,xugou,mori= line.strip().split()
scores_inner = [float(hundun),float(xugou),float(mori)]
#构建value
player_scores[name] = scores_inner##输入value
return player_scores
except FileNotFoundError:
print(f"错误:文件'{scores_game_name}'不存在。")
return {} # 返回空字典
except ValueError:
print("文件格式错误,每行应该包含姓名和三个深渊副本的轮数。")
return {} # 返回空字典
except Exception as e:
print(f"读取文件时发生错误:{e}")
return {} # 返回空字典
def average_players(player_scores):#计算平均成绩
average_scores = {}
for name,scores_inner in player_scores.items():
average_scores = sum(scores_inner)/len(scores_inner)
#player_scores包括key(name),value(scores_inner,是
#[hundun,xugou,mori]的一个list)
return average_scores
def all_players_scores(filename,average_scores):#综合
try:
test = open('深渊轮数.txt','w')
test.write("姓名 平均成绩\n")
for name, average in average_scores.items():
test.write("{} {:.2f}\n".format(name,average))
except ValueError:
print("文件格式错误,每行应该包含姓名和三个深渊副本的轮数。")
return {} # 返回空字典
except Exception as e:
print(f"读取文件时发生错误:{e}")
return {} # 返回空字典
#——————————————————————————————————
scores_game_name = '深渊轮数.txt'
averages_game_name = 'average_scores.txt'
player_scores = read_lunshu(scores_game_name)
average_scores = average_players(player_scores)
result = all_players_scores(scores_game_name,average_scores)
print(result)
#——————————————————————————————————