老男孩python3学习,课堂作业1.1 登录接口

时间:2022-09-20 09:10:04

刚接触python的小白,学习的python3.

编写:登录接口,认证成功显示欢迎信息,输入错误3次锁定账户:

# Version: python3.6
# Author: Gao

# 登录接口,认证成功显示欢迎信息,输入错误3次锁定账户

f = open("work2.txt", 'r')
count_f = f.readlines() # 读取ork2.txt这个文档存放的账户信息内容,readlines自动将文件内容分析成一个行的列表
f.close()
all_name = [] # 初始用户name列表
all_pass = []
for i in range(0, len(count_f)): # 循环count_f中元素个数
count = count_f[i].split() # split拆分字符串
one_name = count[0] # 获取帐号name
one_pass = count[1] # 获取帐号password
all_name.append(one_name) # 放入name列表
all_pass.append(one_pass)
#print(all_name)
# print(all_pass)
count = 0
while count < 3: # 密码错误超过3次账户被锁定,while循环三次
input_name = input("please input you name :").strip()
input_pass = input("please input you password :")
name_Blacklist = input_name + "_Blacklist" # 黑名单账户
if input_name not in all_name: # 账户错误执行
print("Sorry,Account does not exist,Please enter again")
count = 0
continue
elif name_Blacklist in all_name: # 账户在黑名单执行
print("Your has been locked,Please contact the administrator")
break
elif input_name in all_name: # 账户正确执行
username_index = all_name.index(input_name) # 索引出输入账户在账户列表里的位置
password_index = all_pass[username_index] # 获取该账户在password列表里的值
# print(password_index)
if password_index == input_pass: # 判断password是否正确
print("Welcome user:{0} interview!!".format(input_name))
break
else:
print("Password error,Please enter again")
count += 1
if count == 3: # 错误3次进入黑名单
print("You enter more than,Blacklist!")
f_w = open("work2.txt", 'a')
f_w.write("\n"+input_name+"_Blacklist"+" "+"1111")
f_w.close()
break