用Python写一个成绩录入系统管理

在平时比赛的时候,我们在Excel里面写入数据后,需要排名还要进行按钮的点击,小王觉得有点烦,作为强大的编程语言Python,难道不可以吗?答案是,当然可以!

项目说明:程序运行后,提示用户有选项菜单,用户根据菜单进行成绩录入,简单方便,随时查看!

涉及知识:元组列表,条件语句,循环,字符串,简单算法,字典,函数


元组算法

# -*- coding :  utf-8 -*-
# @Time      :  2020/8/8
# @author    :  王小王
# @Software  :  PyCharm
# @CSDN      :  https://blog.csdn.net/weixin_47723732

scores = []
choice = None
print('''
        成绩录入小程序
        0 - 结束程序
        1 - 输入姓名成绩并录入
        2 - 打印排名好后的成绩
        3 - 查找特定学生的成绩
        4 - 删除特定学生的成绩
     ''')
while choice != "0":
    choice = input("Choice: ")
    if choice == "0":
        print("结束程序!")
    elif choice == "1":
        name = input("输入姓名: ")
        score = int(input("输入成绩: "))
        listing = (score, name)
        scores.append(listing)
        scores.sort(reverse=True)
    elif choice == "2":
        print("  成绩排名")
        print("姓名\t成绩")
        for listing in scores:
            score, name = listing
            print(name, "\t", score)
    elif choice == "3":
        Name = input("输入你要查找的姓名:")
        for each in scores:
            if each[1] == Name:
                score ,name=each
                print("姓名\t成绩")
                print(name, "\t", score  )
    elif choice == "4":
        Name = input("输入您要删除成绩的姓名: ")
        for each in scores:
            if each[1] == Name:
                scores.remove(each)
    else:
        print("你输入的选择有误,请检查!")
input("\n\nPress the enter key to exit.")

在这里插入图片描述


字典算法

# -*- coding :  utf-8 -*-
# @Time      :  2020/8/8
# @author    :  王小王
# @Software  :  PyCharm
# @CSDN      :  https://blog.csdn.net/weixin_47723732


# 所有学生数据保存到一个列表,每个学生的信息用一个字典表示[{},{}]
records = []


def print_menu():
    """
    打印出整个菜单
    :return:
    """
    print("""
    学生信息系统

    0 - 退出
    1 - 显示所有学生信息
    2 - 添加学生信息
    3 - 查找学生信息
    4 - 删除学生信息
    5 - 排序
    """)


def user_choice():
    """
    获取用户输入,并确保输入数据在0-4之间
    :return: 返回合法的用户选择
    """
    choice = input("请选择(0~5):")
    # 确保用户的选择是在0~4
    while int(choice) > 5 or int(choice) < 0:
        choice = input("请重新选择(0~5):")

    return choice


def add_record():
    """
    添加用户数据
    :return:
    """
    name = input('请输入学生的名字:')
    while name == '':
        name = input('请输入学生的名字:')

    # 确定学生信息是否已经录入
    for info in records:
        if info['name'] == name:
            print("该学生已经存在")
            break
    else:
        score = float(input('请输入成绩(0~100):'))
        while score < 0 or score > 100:
            score = float(input('请输入成绩(0~100):'))

        info = {
            'name': name,
            'score': score
        }

        records.append(info)


def display_records():
    """
    展示数据
    :return:
    """
    print("所有学生信息")
    for info in records:
        print("姓名:", info['name'], '成绩:', info['score'])


def search_record():
    """
    根据学生名字查找信息,并输出学生的成绩
    :return:
    """
    name = input('请输入学生的名字:')
    while name == '':
        name = input('请输入学生的名字:')

    for info in records:
        if info['name'] == name:
            print("学生成绩为:", info['score'])
            break
    else:
        print("没有该学生")


def del_record():
    """
    根据学生名字删除数据
    :return:
    """
    name = input('请输入学生的名字:')
    while name == '':
        name = input('请输入学生的名字:')

    for info in records:
        if info['name'] == name:
            records.remove(info)
            break
    else:
        print("没有该用户")


choice = None

while choice != "0":
    # 打印菜单
    print_menu()

    # 获取用户输入
    choice = user_choice()

    # 用条件分支去判定各种选择
    if choice == '1':
        # 显示数据
        display_records()
    elif choice == '2':
        # 添加一个学生数据
        add_record()
    elif choice == '3':
        # 查找信息
        search_record()
    elif choice == '4':
        # 删除数据
        del_record()
    elif choice == '5':
        records.sort(key=lambda item: item['score'])

    elif choice == '0':
        # 退出程序
        print('欢迎下次登录本系统')
    else:
        print('选择错误!!!')


在这里插入图片描述
实际操作你们可以自己去试试!

猜你喜欢

转载自blog.csdn.net/weixin_47723732/article/details/107946122