python 第一次作业

时间:2022-10-26 16:42:14

7-5

(a)修改那个脚本,使它能记录用户上次的登录日期和时间(用 time 模块),并与用户密码一起

保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示,
在户名成功登录后,应更新相应用户的上次登录时间戳。如果本次登录与上次登录在时间上相差不
超过 4 个小时,则通知该用户: “You already logged in at: <last_ login_timestamp>.”
(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名

字和他们的密码的清单。

#!/usr/bin/env python
# coding=utf-8
import time
import re
def newuser():
   k=0  print "新用户注册"  name = raw_input("用户名:")
   f=file("./username.txt","r+")
   for user_string in f.readlines():
      user = user_string[0:user_string.find(' ')]
      if name == user:
         k=1  break  if k==0:
      f=file("./username.txt","a+")
      password=raw_input("用户密码:")
      print ("新用户创建成功!")
      login_time = time.strftime("%Y-%m-%d %X",time.localtime())
      user_string = name +" "+ password +"+"+ login_time + '\n'  f.write(user_string)
      f.close()
   else:
      print "ERROR" def login():
   k=0  print "用户登陆"  name = raw_input("用户名:")
   password = raw_input("密码:")
   f=file("./username.txt","a+")
   for user_string in f.readlines():
      user = user_string[0:user_string.find(' ')]
      passwd = user_string[user_string.find(' ')+1:user_string.find('+')]
      last_login = user_string[user_string.find('+')+1:len(user_string)-1]
      if user == name and passwd == password:
         print "登陆成功"  login_time = time.strftime("%Y-%m-%d %X",time.localtime())
         Del(user)
         user_string = name +" "+ password +"+"+ login_time + '\n'  f.write(user_string)
         f.close()
         if login_time[0:10] == last_login[0:10] and int(login_time[11:13]) - int(last_login[11:13]) <=4 :
            print "you already login in 4 hours:%s"%(last_login)
         else :
            print "last login time : %s"%(last_login)
         k=1  showuser()
         break  if k == 0:
      print "ERROR"  f.close()
def showuser():
   while 1:
      print "(d)elete user"  print "(s)how user"  print "(q)uit"  choice=raw_input( "Enter choice:")
      if (choice == 'd') or (choice == 'D'):
         user1=raw_input("请输入要删除的用户名:")
         Del(user1)
         print "%s Successful delete" %user1
      elif (choice == 's') or (choice == 'S'):
         Show()
      elif (choice == 'q') or (choice == 'Q'):
         break def showmenu():
   while 1:
      print "(n)ew user login"  print "(e)xisting user login"  print "(q)uit"  choice=raw_input( "Enter choice:")
      if (choice == 'n') or (choice == 'N'):
         newuser()
      elif (choice == 'e') or (choice == 'E'):
         login()
      elif (choice == 'q') or (choice == 'Q'):
         break  def Del(user):
      f=open("./username.txt",'r')
      userfile = f.readlines()
      f=open("./username.txt",'w')
      for x in userfile:
            if user in x:
                  continue  else:
                  f.write(x)
      f.close()
def Show():
      f=open("./username.txt",'r')
      for user_string in f.readlines():
         print user_string
      f.close()
showmenu()

7-8

人力资源。创建一个简单的雇员姓名和编号的程序。让用户输入一组雇员姓名和编号。
你的程序可以提供按照姓名排序输出的功能,雇员姓名显示在前面,后面是对应的雇员编号。附加
题:添加一项功能,按照雇员编号的顺序输出数据。

#!/usr/bin/env python
#coding=utf-8
count=input("请输入需要创建用户数:")
dic = {}
i = 0
while i < count:
    i +=1
    key = raw_input("请输入用户姓名:")
    if key in dic:
        print "该用户已经存在,请确认后重新输入"
        i -=1
    else:
        num = input("请输入用户编号:")
        if num in dic.values():
            print "编号重复请查证"
        else:
            dic[key]=num
sorted_dic=sorted(dic.iteritems(), key=lambda dic : dic[1])
print sorted_dic