软测笔试编程题一及扩展

使用Python来实现老师询问学生成绩的功能,同时允许老师更新学生成绩。一个简单的实现方式是使用字典来存储学生姓名和成绩的关联。以下是实现代码。

#用字典存放学生的姓名和成绩
student_scores = {}
# 添加学生的姓名和成绩
def add_student_score(name, score):
    student_scores[name] = score

#获取分数最高的学生和分数
def get_student_score():
    if not student_scores:
        return "没有此学生"

    max_score = max(student_scores.values())
    max_student = [name for name, score in student_scores.items() if score == max_score]

    return max_student, max_score
# 打印所有学生的成绩
def print_all_score():
    if not student_scores:
        print("没有学生记录")
    else:
        print("所有学生的成绩:")
        for name, score in student_scores.items():
            print(f"{name} : {score}")

# 主程序
while True:
    print("1、添加学生的成绩")
    print("2、获取分数最高的学生和成绩")
    print("3、打印所有学生的成绩")
    print("4、继续执行")
    print("5、退出")

    choice = input("请选择操作1/2/3/4/5:")

    if choice == '2':
        max_student, max_score = get_student_score()
        if isinstance(max_student, list):
            print(f"分数最高的学生是:{max_student},分数为:{max_score}")

        else:
            print(max_student)

    elif choice == '1':
        name = input("输入学生的姓名")
        score = int(input("输入该学生的成绩"))
        if name in student_scores:
            student_scores[name] = score
            print(f"{name}的成绩已经更新成{score}")
        else:
            add_student_score(name, score)
            print(f"新添加了{name}的分数为{score}")

    elif choice == '3':
        print_all_score()

    elif choice == '4':
        continue

    elif choice == '5':
        break

    else:
        print("无效的选择,请重新输入!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值