python学生信息管理


#record student infomation
def input_student(L):
    while True:
        name = input("please input student name:")
        if not name:
            return L
        age = input("please input age:")
        score = input("input score:")
        stu = {"name":name, "age":age,"score":score}
        L.append(stu)
    return L

# print student information
def output_student(lst):
    s_frame = '+'+'-'*20+'+'+'-'*20+'+'+'-'*20+'+'
    s_title = '|{:^20}|{:^20}|{:^20}|'.format('NAME','AGE','SCORE')
    print(s_frame)
    print(s_title)
    print(s_frame)
    for I in lst:
        s_item = '|{:^20}|{:^20}|{:^20}|'.format(I['name'],I['age'],I['score'])
        print(s_item)
        print(s_frame)
    return
#删除学生信息
def delete_menu(L):
    s = input('delete student name:')
    for stu in L:
        if stu['name'] == s:
            L.remove(stu)
    return L

#修改学生成绩
def modify_score(L):
    print("modify student score.")
    name = input('stu name:')
    score = int(input('stu score:'))
    for d in L:
        if d['name'] == name:
            d['score'] = score
    return L

def sort_high_low(L):
    print("按照成绩由高到低显示学生信息:")
    return sorted(L, key = lambda d: int(d['score']), reverse = True)


def sort_low_high(L):
    print("按照成绩由低到高显示学生信息;")
    return sorted(L, key = lambda d: d['score'], reverse = False)

def sort_age_HL(L):
    print("按照年龄由高到低显示学生信息")
    return sorted(L, key = lambda d: d['age'], reverse = True)

def sort_age_LH(L):
    print("按照年龄由低到高显示学生信息")
    return sorted(L, key = lambda d: d['age'], reverse = False)


# Operation Menu
def menu_stu():
    L = []
    s_menu = '''
    1) 添加学生信息
    2)显示所有学生信息
    3) 删除学生信息
    4) 修改学生成绩
    5) 按照学习成绩高-低显示学生信息
    6)按照学习成绩低-高显示学生信息
    7)按照学生年龄高-低显示学生信息
    8)按照学生年龄低-高显示学生信息
    q) 退出
    '''
    while True:
        print(s_menu)
        choice = input(">>>")
        if choice == '1':
            L = input_student(L)
        elif choice == '2':
            output_student(L)
        elif choice == '3':
            L = delete_menu(L)
        elif choice == '4':
            L = modify_score(L)
        elif choice == '5':
            L = sort_high_low(L)
        elif choice == '6':
            L = sort_low_high(L)
        elif choice == '7':
            L = sort_age_HL(L)
        elif choice == '8':
            L = sort_age_LH(L)
        elif choice == 'q':
            return
        else:
            print("input error!!!")
#主程序
def main():
    menu_stu()
main()

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值