人力资源。创建一个简单的雇员姓名和编号的程序。让用户输入一组雇员姓名和编号。你的程序可以提供按照姓名排序输出的功能,雇员姓名显示在前面,后面是对应的雇员编号。附加题:添加一项功能,按照雇员编号的顺序输出数据。
#!/usr/bin/evn python
#coding:utf-8
dict1={}
dict2={}
def add():
i = 0
count = input("Enter total number of names: ")
while i < count:
name=raw_input("please input employee name: ")
if name=='--':
break
number=raw_input("please input a employee number:")
dict1[name]=number
dict2[number]=name
i += 1
xuanze()
def xuanze():
print '''
1.插入数据
2.按姓名排序
3.按编号排序
4.退出'''
a = raw_input("what do you want to do: ")
if a == "1":
add()
elif a == "2":
for key in sorted(dict1):
print key,dict1[key]
elif a == "3":
for key in sorted(dict2):
print key,dict2[key]
elif a == "4":
quit
else:
print "input error"
pass
if __name__ == '__main__':
xuanze()
下面的问题和例题 7.1 中管理名字-密码的键值对数据的程序有关。
(a)修改那个脚本,使它能记录用户上次的登录日期和时间(用 time 模块),并与用户密码一起保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示,在户名成功登录后,应更新相应用户的上次登录时间戳。 如果本次登录与上次登录在时间上相差不超过 4 个小时,则通知该用户: “You already logged in at: <last_ login_timestamp>.”
(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名字和他们的密码的清单
#!/usr/bin/env python#coding:utf-8
from time import time,ctime
dic = {}
def user():
name=raw_input("please input username: ")
if name in dic:
passwd=raw_input("please input password: ")
if passwd == dic.get(name)[0]:
print 'Holle,welcome back',name
print 'You last login time at:',ctime(dic[name][1])
now=time()
timediff=now-dic[name][1]
if timediff <= 14400:
print "You already logged within 4 hours!"
else:
logintime=time()
dic[name][1]=logintime
else:
print "password error!"
else:
a = raw_input("the name is not exit!do you want to login it:yes/no: ")
if a == "yes":
while True:
name = raw_input("please input your name: ")
if dic.has_key(name):
a = raw_input("User name already exists, please input again: ")
continue
else:
break
passwd=raw_input("please input password: ")
logintime=time()
dic[name]=[passwd,logintime]
if a == "no":
user()
xuanze()
def delete():
name=raw_input("Please enter the user you want to delete: ")
if name in dic:
del dic[name]
else:
print("the name is not exit!please input again: ")
delete()
xuanze()
def xuanze():
print'''
1.登陆界面
2.删除用户
3.显示所有用户
4.退出'''
a = raw_input("what do you want to do: ")
if a == "1":
user()
elif a == "2":
delete()
elif a == "3":
print dic
xuanze()
elif a == "4":
quit
else:
print "input error"
pass
if __name__ == '__main__':
xuanze()