python练习
创建一个简单的姓名和编号系统,让用户输入一组人的姓名和编号,实现提供按照编号或姓名排序输出的功能。
nums = [] names = [] Afternums = [] Afternames = [] Afternums1 = [] Afternames1 = [] Afternums2 = [] Afternames2 = [] Afternums3 = [] Afternames3 = [] def init(): while(1): num = int(input('please enter a number:')) name = input('please enter the name:') nums.append(num) names.append(name) if(input('是否继续输入,退出请按q:') == 'q'): break def sortnumer(): num = nums[:] name = names[:] while(len(num)): Minnum = min(num) Minname = name.pop(num.index(Minnum)) Afternums.append(Minnum) Afternames.append(Minname) num.remove(Minnum) print("编号从小到大:") print(30*"*") for i in range(len(Afternums)): print('%15d %-20s' % (Afternums[i],Afternames[i])) print(30*"*") def sortnumer1(): num1 = nums[:] name1 = names[:] while(len(num1)): Maxnum = max(num1) Maxname = name1.pop(num1.index(Maxnum)) Afternums1.append(Maxnum) Afternames1.append(Maxname) num1.remove(Maxnum) print("编号从大到小:") print(30*"*") for i in range(len(Afternums1)): print('%15d %-20s' % (Afternums1[i],Afternames1[i])) print(30*"*") def sortname(): num2 = nums[:] name2 = names[:] while(len(name2)): Minname = min(name2) Minnum = num2.pop(name2.index(Minname)) Afternums2.append(Minnum) Afternames2.append(Minname) name2.remove(Minname) print("姓名从小到大:") print(30*"*") for i in range(len(Afternames2)): print('%15s %-10d' % (Afternames2[i],Afternums2[i])) print(30*"*") def sortname1(): num3= nums[:] name3 = names[:] while(len(name3)): Maxname = str(max(name3)) Maxnum = num3.pop(name3.index(Maxname)) Afternums3.append(Maxnum) Afternames3.append(Maxname) name3.remove(Maxname) print("姓名从大到小:") print(30*"*") for i in range(len(Afternames3)): print('%15s %-10d' % (Afternames3[i],Afternums3[i])) print(30*"*") def main(): print(20*"*"+"欢迎进入人力资源档案系统"+20*"*") print("请输入一组职员名称和编号:") init() print("请选择查看的方式") print("1.按照编号从小到大输出") print("2.按照编号从大到小输出") print("3.按照姓名从小到大输出") print("4.按照姓名从大到小输出") print("0.退出请按0") while(1): index = int(input("请选择:")) if(index==1): sortnumer() elif(index==2): sortnumer1() elif(index==3): sortname() elif(index==4): sortname1() elif(index==0): print("您已退出,谢谢使用") break else: print("输入有误,请重试") main() >>> ================================ RESTART ================================ >>> ********************欢迎进入人力资源档案系统******************** 请输入一组职员名称和编号: please enter a number:5659 please enter the name:zhaoyujiao 是否继续输入,退出请按q: please enter a number:8965 please enter the name:songlin 是否继续输入,退出请按q: please enter a number:1234 please enter the name:xiaohua 是否继续输入,退出请按q:q 请选择查看的方式 1.按照编号从小到大输出 2.按照编号从大到小输出 3.按照姓名从小到大输出 4.按照姓名从大到小输出 0.退出请按0 请选择:1 编号从小到大: ****************************** 1234 xiaohua 5659 zhaoyujiao 8965 songlin ****************************** 请选择:2 编号从大到小: ****************************** 8965 songlin 5659 zhaoyujiao 1234 xiaohua ****************************** 请选择:3 姓名从小到大: ****************************** songlin 8965 xiaohua 1234 zhaoyujiao 5659 ****************************** 请选择:4 姓名从大到小: ****************************** zhaoyujiao 5659 xiaohua 1234 songlin 8965 ****************************** 请选择:5 输入有误,请重试 请选择:0 您已退出,谢谢使用 >>>