python——登陆接口设计(循环方法)

时间:2023-03-09 08:23:26
python——登陆接口设计(循环方法)

近日重新整理了登陆接口设计程序,感觉以前的代码没有注释,让园子的其他童鞋读起来比较费劲。也没有流程图和程序运行说明。

1.流程图

python——登陆接口设计(循环方法)

2.user_file.txt&lock_file.txt文件内容

(1) user_file.txt

Abel 123
Bbel 1234
Cbel 123456

(2) lock_file.txt

Dbel

3.程序运行说明

(1)输入用户名,程序对比lock_file.txt。如果存在则提示该用户已经被锁定,退出程序。

(2)程序查找用户名是否在user_file.txt中,如果不在提示用户,并退出程序。

(3)用户输入密码,连续输入三次以内,密码正确。提示欢迎,并退出程序。

(4)密码连续输入错误3次,提示用户已经被锁定,并将用户名写入lock_file.txt中。退出程序。

4.程序代码

 import os

 user_file = open('use_file.txt', 'r')  # 打开user_file.txt
user_list = user_file.readlines() # 一次性将user_file.txt中的内容加载到内存中
user_file.close() # 关闭user_file.txt while True:
lock_file = open('lock_file.txt', 'r+') # 打开lock_file.txt
lock_list = lock_file.readlines() # 将lock_file.txt中的内容加载到内存中
lock_file.close() # 关闭lock_file.txt login_Success = False # 设置标记位,用于跳出循环
user_name = input('Please enter your name:'.strip()) # 输入用户名
for line1 in lock_list:
line1 = line1.split() # 将lock_file.txt中的信息读取到line1中
if user_name == line1[0]: # 如果用户名在line1中提示信息并退出整个程序
print("对不起!您的用户名已经被锁定,请联系网站管理员。")
exit()
for line2 in user_list:
line2 = line2.split() # 将user_file.txt中的信息读取到line2中
if user_name == line2[0]: # 如果用户名在line2中进入for循环(输入密码三次错误锁定)
for i in range(3): # 计数器,记录密码输入错误次数
password = input('Please enter your password'.strip()) # 输入密码
if password == line2[1]: # 如果password在line2[1]中,显示欢迎信息,并退出整个程序
print("欢迎 %s 登陆Abel网站!" % user_name)
login_Success = True
break
else: # 密码输入错误次数超过3次,将用户名写入lock_file.txt中
f = open('lock_file.txt', 'a')
f.write('%s\n' % user_name)
f.close()
print("连续输入3次错误密码,您的用户%s已经被锁定,请联系网站管理员。" % user_name) # 提示用户已经锁定,并退出整个程序
login_Success = True
break
if login_Success:
break
else: # 用户名不在line2中,提示用户名不存在。并退出整个程序
print("您输入的用户名不存在,请重新输入或注册")
exit()
if login_Success:
break