用户登录验证

本程序在suse11sp3  python2.6.8 中运行通过, 在pyhon3以上的版本中只需要把 raw_input 改成input 即可。svn 中已经把  raw_input 改成了 input 。
userinfo 文件每一行记录用户的账号,密码,输入密码的次数,用逗号分开。如下第一行表示账号是 kavin Garnett , 密码是 a!1#$ , 输入密码次数 0 (账号未锁定), 如果最后一列是3 ,则表示该账号已锁定。

 

程序如下:

 

 

#/usr/bin/env python #-*- coding: utf-8 -*- #--author: zhoudingzhao #用户账号,密码,登录次数存储在userinfo文件中,以逗号分隔。 f = open("userinfo","r+") line = f.readlines(); f.close() #读取文件,保存在 userdict 字典中。 userdict= {} i = 0 while i < len(line):   for lines in line:     lines = line[i].strip('\n').split(',')     key1 = lines[0]     key11 = lines[1]     value11 = lines[2]     userdict11 = {}     userdict11[key11] = int(value11)     userdict[key1] = userdict11     i = i + 1 print (userdict)

name = input("input your name:") if name in userdict.keys():    trynum = userdict[name].values()[0]                         #账号输入正确时,记录登录时,输入密码次数    while (trynum < 3):         pwd = input("input your password:")       if pwd == (userdict[name].keys())[0]:                    #输入密码次数小于3次,并且密码输入正确,打印登录成功,跳出循环。          print("login successful!")          break       else:                                                    #输入密码次数小于3次,并且密码输入错误,继续尝试输入密码。          print("password is wrong ,please input password:")          trynum = trynum +1    if(trynum >= 3):                                            #输入密码次数大于等于3次,把改账号锁定。       print ("your account is locked")       userinfo = open("userinfo","w")                          #把字典写入userinfo中,并且把锁定的账号的登录次数改成 3 。       for key in userdict:           userinfo.write(key)           for key2 in userdict[key]:               if key == name:                  userinfo.write(',')                  userinfo.write(key2+',')                  strnum = str(trynum)                  userinfo.write(strnum+'\n')               else:                  userinfo.write(',')                  userinfo.write(key2+',')                  strvalue = str(userdict[key][key2])                  userinfo.write(strvalue+'\n') else:    print("your name is wrong")

你可能感兴趣的:(用户登录验证)