zoukankan      html  css  js  c++  java
  • 老男孩-day1作业一

    练习程序:编写登录接口

    1. 输入用户名和密码

    2. 认证成功后显示欢迎信息

    3. 输错三次后锁定

    程序思路:

    1. 用文件user_database.txt当做数据库文件,用于验证用户、密码、密码错误次数、锁定状态。

        初始数据为:

    admin password 1 1
    user2 user2password 0 0
    user1 user1password 1 0

    2. 程序中f1的定义部分为,从文件中读取数据,并且生成字典。(此处本该使用函数的)

    3. save_file() 函数,用于将字典保存到user_database.txt

    4. 其余部分为判断用户和密码,错误次数、锁定状态等。

    代码程序为:

     1 # coding: gbk
     2 
     3 # 定义将字典保存到文件user_database.txt的函数
     4 def save_file(dic):
     5     file_clear = open("F://PythonDay3//task//day1//user_database.txt","w")
     6     file_clear.close()
     7     for i in dic.items():
     8         item,value = i
     9         value1,value2,value3 = value    
    10         file_open = open("F://PythonDay3//task//day1//user_database.txt","a")
    11         file_open.write(item+" "+value1+" "+value2+" "+value3+"
    ")
    12         file_open.close()
    13 
    14 
    15 # 从文件中取出数据,并以字典形式体现。字典格式为{"admin":["password","0","0"]}   分别代表的意思:用户、密码、错误次数、是否锁定(0代表未锁,1代表锁)
    16 f1 = open("F://PythonDay3//task//day1//user_database.txt","r")
    17 dic_user = {}
    18 for line in f1:
    19     user_name,user_pwd,lock_times,locked_status = line.split(" ")
    20     dic_user[user_name] = [user_pwd,lock_times,locked_status.strip("
    ")]
    21 f1.close()
    22 
    23 
    24 # 登录。通过while进行无限制循环,通过try进行异常处理,在使用Ctrl+C中断的时候,保存字典到文件。其余为判断用户和密码以及次数。
    25 while 1:
    26     try:
    27         Input_user = input("请输入用户名:")        
    28         Input_pswd = input("请输入密码:")
    29         if Input_user in dic_user.keys() and dic_user[Input_user][0] == Input_pswd:
    30             if dic_user[Input_user][2] == "0":
    31                 print("You have logined")
    32                 dic_user[Input_user][1] = "0"
    33                 dic_user[Input_user][2] = "0"
    34                 save_file(dic_user)
    35             elif dic_user[Input_user][2] == "1":
    36                 print("User %s is locked! Please contact adminstrator! Quiting" % Input_user)
    37                 exit()
    38         elif Input_user in dic_user and dic_user[Input_user][0] != Input_pswd:
    39             if dic_user[Input_user][2] == "1":
    40                 print("User %s is locked! Please contact adminstrator! Quiting" % Input_user)
    41                 exit()
    42             dic_user[Input_user][1] = str(int(dic_user[Input_user][1]) + 1)
    43             if int(dic_user[Input_user][1]) >=3:
    44                 print("您的密码输入错误 %s 次,已经锁定!请联系管理员" % dic_user[Input_user][1])
    45                 dic_user[Input_user][2] = "1"
    46                 dic_user[Input_user][1] = "0"
    47                 save_file(dic_user)
    48                 exit()
    49             else:
    50                 print("您的密码输入错误 %s 次" % dic_user[Input_user][1])
    51         else:
    52             print("没有该用户")
    53     except KeyboardInterrupt:
    54         save_file(dic_user)
    55         exit()
  • 相关阅读:
    vi 编辑内容中查找字符位置
    使用 Oracle GoldenGate 在 Microsoft SQL Server 和 Oracle Database 之间复制事务
    EBS R12 更改SYSADMIN密码
    两种步骤 更改 EBS R12界面LOGO以及内容
    Get Current LOV Query SQL
    Oracle Gateways透明网关访问SQL Server
    Oracle 数据集成的实际解决方案
    将SQLServer2005中的数据同步到Oracle中
    ogg实现oracle到sql server 2005的同步
    wince和window mobile winphone
  • 原文地址:https://www.cnblogs.com/loyfee/p/5690445.html
Copyright © 2011-2022 走看看