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()

 

你可能感兴趣的:(python学生信息管理,Python)