Python列表知识实现员工信息管理系统
本帖最后由 233倔强不秃 于 2020-2-25 02:07 编辑一、引言:
相信很多小伙伴都曾经用C语言或者C++写过学生信息管理系统,在那个指针,结构体,数组,链表等等满天飞的时候,本来学习就不好的我更是抓耳挠腮,一个程序写下来全是bug。学习Python有一个星期了,据说Python是最便捷的语言,我突发奇想再尝试写一次大一时候的阴影——员工信息管理系统。
二、源代码呈上:
#员工信息管理系统(Employee Manage System )
#欢迎界面
print('='*50)
print('\t\t\t'+'Employee Manage System'+'\t')
print('\t\t\t\t'+'Welcome!'+'\t\t')
print('\t\t\t'+'1.seek employee '+'\t')
print('\t\t\t'+'2.add employee'+'\t')
print('\t\t\t'+'3.delete employee'+'\t')
print('\t\t\t'+'4.All of employees'+'\t')
print('\t\t\t'+'5.quit'+'\t')
print('='*50)
employees = [['孙悟空','男','花果山'],['猪八戒','男','高老庄'],['沙和尚','男','流沙河'],['唐三藏','男','东土大唐']]
while True:
s = input('What can I do for you?') #判断输入是否合法
if s.isdigit():
choose = int(s)
if choose == 1: #part 1
print('-'*50)
num = len(employees)
name = input('Please enter employee\'s name:')
flag = 0
for i in range(0,num):
if name in employees:
print('\t\t\t'+'*'*40+'\t')
print('\t\t\t'+' num\t name\t sex\t address'+'\t')
print('\t\t\t',i+1,'\t',employees,'\t',employees,'\t',employees,'\t')
print('\t\t\t'+'*'*40+'\t')
flag = 1
break
else:
continue
if flag == 0:
print('He or she is not here.')
elif choose == 2: #part 2
print('-'*80)
person =
employees.append(person)#用append增添列表新成员
person = input('Please enter employee\'s name:')
person = input('Please enter employee\'s sex:')
person = input('Please enter employee\'s address:')
print('Add employee successfully!')
elif choose == 3: #part 3
print('-'*80)
num = len(employees)
name = input('Please enter his or her name:')
flag = 0
for i in range(0,num):
if name in employees:
del employees
print('Delete employee successfully!')
flag = 1
break
else:
continue
if flag == 0:
print('He or she is not here.')
elif choose == 4: #part 4
print('-'*80)
print('\t'+'All of employees:'+'\t')
num = len(employees)
for i in range(0,num):
print('\t\t\t'+'*'*40+'\t')
print('\t\t\t'+' num\t name\t sex\t address'+'\t')
print('\t\t\t',i+1,'\t',employees,'\t',employees,'\t',employees,'\t')
print('\t\t\t'+'*'*40+'\t')
elif choose == 5: #part 5
input('Enter to quit...') #点击回车退出
break
else:
print('input error! Please enter 1-5!')
print('-'*80)
else:
print('input error! Please enter 1-5!')
运行结果展示见图片。
三、心得:
1.主体部分:
主体部分还是用if...elif...else语句来书写,和C语言中的switch比起来可能稍微麻烦一丢丢。因为要循环输入,所以将主体嵌套在while循环中,从上到下顺序走完,也没使用函数的知识,所以顺序结构很普通。
2.功能部分:
按我们以往的C语言思路,应该创建结构体, 然后用结构体数组存放每个员工的信息,如果要考虑容纳人数的上限很大时,可能还要用到链表。而用C++时,需要创建对象,实现存储以及增删查改功能,可能还要用到容器。但是在python中,最简单的列表就可以实现。不得不说,这实在是python的过人之处。
我的思路是先建立一个列表来存放每一个员工的个人信息(即person),再将所有员工都存进一个列表(employees),每次访问的时候可以直接用employees来访问一个员工的信息,或者用employees这样的形式访问员工信息中的每一个数据。而且使用列表还可以调用list的BIF实现快捷功能。
功能一,查找:
用for循环在employees列表中,以employees的形式查找信息匹配的数据,并按格式打印。
功能二,增加:
调用列表的append()方法,一次插入一个列表(person),将新成员信息插入employees列表。
功能三,删除:
直接调用 def语句,进行删除。
功能四,遍历:
for循环解决
功能五,退出:
break语句退出循环。
3.细节部分:
第一就是处理非法输入上,调用了字符串的isdigit()方法,判断输入的是否是数字,其次通过条件判断是否为1-5的合法选项,如果不是则提示重新输入。
第二就是页面优化,以表格的形式打印等。还了解到了一个比较容易忽略的问题:print('\t'+变量/序列等)这种形式是不允许的,“+”号连接的只能是字符串类型,其他类型则用“,”隔开。
四、总结:
虽然学习python的时间很短,学的也很浅很基础,但是我相信多动手多思考一定是能学好的,同时感谢小甲鱼的精品课程视频,讲的很好。最后,欢迎大家对我的代码或者文章中的不足之处进行提问和讨论!
https://fishc.com.cn/forum.php?mod=viewthread&tid=52272&extra=page%3D1%26filter%3Dtypeid%26typeid%3D441 zltzlt 发表于 2020-2-8 17:58
https://fishc.com.cn/forum.php?mod=viewthread&tid=52272&extra=page%3D1%26filter%3Dtypeid%26typeid%3D ...
谢谢,学到了,第一次发帖,没注意好格式。{:5_110:}
页:
[1]