You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python实现文件内容搜索及高尔夫记录修改功能

高尔夫球员成绩管理程序补全方案

核心实现逻辑

你当前的存储规则是每两行对应一条球员记录:奇数行存姓名、偶数行存成绩,基于这个规则实现搜索、修改功能的逻辑如下:

  • 搜索功能:逐行读取文件内容,成对读取姓名、成绩字段,和输入的待搜索姓名做比对,匹配到立即输出对应记录即可,全部遍历完无匹配则提示未找到
  • 修改功能:文本文件无法直接修改局部内容,需要先把所有记录读取到内存中,定位到待修改的记录后更新成绩,再把全量记录覆盖写入原文件完成修改

补全后完整代码

def main():
    move_on = 'y'
    golfScoreFile = open('golfRecords.txt', 'a')
    while move_on == 'y' or move_on == 'Y':
        pName = input("Enter the player's name: ")
        pScore = int(input("Enter the player's score: "))
        golfScoreFile.write(pName + "\n")
        golfScoreFile.write(str(pScore) + "\n")
        move_on = input("Would you like to continue? [y]: ")
    displayRecords = input("Would you like to display all the records from golfRecords.txt? [y]: ")
    golfScoreFile.close()
    if (displayRecords == 'y' or displayRecords == 'Y'):
        displayRecs()
    else:
        # 搜索后可选择是否修改
        target_name = searchRecs()
        if target_name:
            mod_confirm = input("是否需要修改该球员的成绩?[y]: ")
            if mod_confirm.lower() == 'y':
                modscore(target_name)


def displayRecs():
    golfScoreFile = open('golfRecords.txt', 'r')
    lineCount = 1
    for line in golfScoreFile:
        golfRecLine = lineCount % 2
        if (golfRecLine == 0):
            print("The score of this person is: ", line.strip())
        if (golfRecLine == 1):
            print("The name of this person is: ", line.strip())
        lineCount = lineCount + 1
    golfScoreFile.close()


def searchRecs():
    search = input("Enter players name to search: ").strip()
    found = False
    with open('golfRecords.txt', 'r') as f:
        while True:
            name_line = f.readline()
            score_line = f.readline()
            # 读到文件末尾结束循环
            if not name_line or not score_line:
                break
            current_name = name_line.strip()
            current_score = score_line.strip()
            if current_name == search:
                print(f"找到匹配记录:球员姓名 {current_name},成绩 {current_score}")
                found = True
                return current_name
    if not found:
        print(f"未找到姓名为{search}的球员记录")
        return None

def modscore(target_name):
    new_score = input("请输入该球员的新成绩:").strip()
    all_records = []
    # 先读取所有记录到内存
    with open('golfRecords.txt', 'r') as f:
        while True:
            name_line = f.readline()
            score_line = f.readline()
            if not name_line or not score_line:
                break
            current_name = name_line.strip()
            current_score = score_line.strip()
            if current_name == target_name:
                # 匹配到目标记录,更新成绩
                all_records.append((current_name, new_score))
            else:
                all_records.append((current_name, current_score))
    # 把全量记录写回文件
    with open('golfRecords.txt', 'w') as f:
        for name, score in all_records:
            f.write(name + "\n")
            f.write(score + "\n")
    print("成绩修改成功")

main()

注意事项

  • 代码默认姓名为唯一标识,若存在重名球员只会修改第一条匹配到的记录
  • 读取、写入时都做了换行符去除处理,避免比对、输出时出现格式问题

内容的提问来源于stack exchange,提问作者mas284

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 06:15:05