Python学习---------登陆系统代码实现

题目要求:

一、编写登陆入口

 1.输入用户名密码
2.认证成功后显示欢迎的信息
3.输错三次后锁定

Readme:

              1.本次实现了登陆系统,若锁定就输出为锁定用户(锁定信息保存在user_lock.txt中,以每个用户为一行存储);若没有锁定就输入密码,判断密码对错(账户保存在user_info.txt文件中,以账户:密码存储为一行依次存储)若正确输出欢迎进入系统;若错误三次锁定用户。

              2.之前没有找到能够循环找出第二、三以及跟多的用户的,

              3.txt文件建立好,信息按照上面存储,运行即可

思维导图:

代码如下:

 1 # -*- Coding:utf-8 -*-
 2 # Author:Eric.Shen
 3 #2018.02.06
 4 # 一、编写登陆入口
 5 #   1.输入用户名密码
 6 #     2.认证成功后显示欢迎的信息
 7 #     3.输错三次后锁定
 8 
 9 print("""
10 ***************************************************************
11 **                                                           **
12 **                                                           **
13 **              Welcome into the landing system              **
14 **                                                           **
15 **                                             by:Eric.Shen **
16 ***************************************************************
17 """)
18 
19 #判断是否为锁定用户
20 def judge_lock_user():
21     username = input("请输入你的用户名:")
22     f = open("user_lock.txt","r+")
23     for line_list in f.readlines():
24         if username == line_list:
25            exit("你的账户已经被锁定!")
26     f.close()
27     return username#返回刚才输入的用户名字,供下面使用
28 
29 def judge_password(username):
30     num = 0
31     while num < 3:
32         password = input("请输入你的密码:")
33         f_user = open("user_info.txt", "r")
34         for line in f_user.readlines():
35             line = line.strip()#去掉最后的换行符
36             index,item = line.split(":")  # 以冒号为分解赋值
37             if index == username and item == password:
38                 f_user.close()
39                 exit("欢迎%s进入系统" % (username))
40         if num <3:
41             num += 1
42             if num <= 2:#没有机会就不输出下面这句话了
43                print("密码错误,请重新输入,你还有%s此输入机会" % (3 - num))
44             elif num == 3:#写入user_lock
45                f_lock = open("user_lock.txt","a")
46                f_lock.write("
"+str("%s"%(username)))
47                f_lock.close()
48                exit("你的账户已经被锁定")
49     else:
50         print("程序退出")
51 
52 
53 
54 
55 
56 
57 if __name__ == "__main__":
58     #判断是否为锁定用户
59     name = judge_lock_user()
60 
61     judge_password(username = name)
62     #不是,则判断密码是否正确
View Code
Powers a lot like real estate.Its all about location, location, location.The closer you are to the source,the higher your property value.
原文地址:https://www.cnblogs.com/zhengyuan/p/8422446.html