defsearch_stu(): "按照学号或姓名查找某一位学生的具体信息" find = -1 t = PrettyTable(["学号","姓名","性别","宿舍房间号","联系电话"]) sea = input("请输入要搜索的学号或姓名: ") if sea.isdigit() == True: for i inrange(len(stu_info)): if stu_info[i][0] == sea: find = i t.add_row(stu_info[i]) if sea.isalpha() == True: count = 0 for i inrange(len(stu_info)): if stu_info[i][1] == sea: find = i t.add_row(stu_info[i]) if find == -1: print("抱歉,未查找到该学生。") else: print(t)
add_stu函数
defadd_stu(): "录入新的学生信息" print("-"*50) print("新增学生") num = input("请输入学号: ") while num.isdigit() != True: num = input("输入错误,请重新输入: ") name = input("请输入姓名: ") while name.isalpha() != True: name = input("输入错误,请重新输入: ") sex = input("请输入性别:(男/女) ") while sex != "男"and sex != "女": sex = input("输入错误,请重新输入: ") room_no = input("请输入房间号: ") while room_no.isdigit() != True: room_no = input("输入错误,请重新输入: ") tel = input("请输入电话:") while tel.isdigit() != True: tel = input("输入错误,请重新输入: ") stu = [num, name, sex, room_no, tel] stu_info.append(stu) print("添加"+num+"成功")
show_all_students函数
defshow_all_students(): for i inrange(len(stu_info)): table.add_row(stu_info[i]) print(table)