|
|
import os
|
|
|
|
|
|
|
|
|
soldier_scores = []
|
|
|
DATA_FILE = "soldier_scores.txt"
|
|
|
def load_data():
|
|
|
global soldier_scores
|
|
|
if not os.path.exists(DATA_FILE):
|
|
|
return False
|
|
|
try:
|
|
|
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
|
|
for line in f.readlines():
|
|
|
line = line.strip()
|
|
|
if line:
|
|
|
name, three_km, push_up = line.split(',')
|
|
|
soldier_scores.append({
|
|
|
'name': name,
|
|
|
'3km': three_km,
|
|
|
'push_up': int(push_up)
|
|
|
})
|
|
|
return True
|
|
|
except Exception as e:
|
|
|
print(f"数据加载失败:{e}")
|
|
|
return False
|
|
|
def input_score():
|
|
|
global soldier_scores
|
|
|
while True:
|
|
|
name = input("请输入士兵姓名(输入 qwer 退出录入):")
|
|
|
if name.lower() == 'qwer':
|
|
|
break
|
|
|
three_km = input("请输入3公里成绩(格式 分:秒,如 12:30):")
|
|
|
try:
|
|
|
push_up = int(input("请输入单杠一成绩(个数):"))
|
|
|
except ValueError:
|
|
|
print("单杠一成绩必须是整数,请重新录入!")
|
|
|
continue
|
|
|
soldier_scores.append({
|
|
|
'name': name,
|
|
|
'3km': three_km,
|
|
|
'push_up': push_up
|
|
|
})
|
|
|
print(f"{name} 的成绩录入成功!")
|
|
|
def search_and_rank():
|
|
|
if not soldier_scores:
|
|
|
print("暂无成绩数据,请先录入!")
|
|
|
return
|
|
|
while True:
|
|
|
choice = input("\n查询功能选择:1-按姓名查询 2-单杠一排名 3-返回主菜单\n请输入数字:")
|
|
|
if choice == '1':
|
|
|
target_name = input("请输入要查询的士兵姓名:")
|
|
|
found = False
|
|
|
for score in soldier_scores:
|
|
|
if score['name'] == target_name:
|
|
|
print(f"\n【{target_name} 训练成绩】")
|
|
|
print(f"3公里:{score['3km']}")
|
|
|
print(f"单杠一:{score['push_up']} 个")
|
|
|
found = True
|
|
|
break
|
|
|
if not found:
|
|
|
print(f"未找到 {target_name} 的成绩数据!")
|
|
|
elif choice == '2':
|
|
|
sorted_list = sorted(soldier_scores, key=lambda x: x['push_up'], reverse=True)
|
|
|
print("\n【单杠一成绩排名】")
|
|
|
print("排名\t姓名\t\t单杠一个数")
|
|
|
print("-" * 30)
|
|
|
for idx, score in enumerate(sorted_list, 1):
|
|
|
print(f"{idx}\t{score['name']}\t\t{score['push_up']}")
|
|
|
elif choice == '3':
|
|
|
break
|
|
|
else:
|
|
|
print("输入错误,请选择 1/2/3!")
|
|
|
def save_data():
|
|
|
if not soldier_scores:
|
|
|
return "暂无数据可保存!"
|
|
|
try:
|
|
|
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
|
|
for score in soldier_scores:
|
|
|
line = f"{score['name']},{score['3km']},{score['push_up']}\n"
|
|
|
f.write(line)
|
|
|
return "数据已成功保存到 soldier_scores.txt!"
|
|
|
except Exception as e:
|
|
|
return f"数据保存失败:{e}"
|
|
|
def main():
|
|
|
load_result = load_data()
|
|
|
print(f"数据加载:{'成功' if load_result else '无历史数据'}")
|
|
|
print("\n===== 士兵训练成绩管理系统 =====")
|
|
|
while True:
|
|
|
print("\n主菜单:")
|
|
|
print("1-录入训练成绩")
|
|
|
print("2-查询与排名统计")
|
|
|
print("3-保存数据到文件")
|
|
|
print("4-退出系统")
|
|
|
choice = input("请输入功能编号:")
|
|
|
if choice == '1':
|
|
|
input_score()
|
|
|
elif choice == '2':
|
|
|
search_and_rank()
|
|
|
elif choice == '3':
|
|
|
print(save_data())
|
|
|
elif choice == '4':
|
|
|
print("感谢使用,再见!")
|
|
|
break
|
|
|
else:
|
|
|
print("输入错误,请选择 1-4 的数字!")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main() |