作业二:编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
http://www.cnblogs.com/alex3714/articles/5465198.html
自己写的第一个长一点的Python程序,当作纪念啦~
要在e盘里面有四个文件才可以运行哦~
1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:Ku 4 5 record_num = open(r'e:first week ecord_num.txt', 'r') # 用来记录用户密码以及尝试过的用户的数量 6 7 # 提取用户数量以及尝试过的用户名数量 8 num_username = int(record_num.readline().rstrip()) 9 num_record_username = int(record_num.readline().rstrip()) 10 #print(num_username,num_record_username) 11 record_num.close() 12 13 count = 1 #指示尝试的次数 14 15 while count <= 3: 16 17 username = input("Please enter your username:") #输入模块 18 #password = getpass.getpass("Please enter your password:") 19 password = input("Please enter your password:") 20 21 22 user = open(r'e:first weekusername.txt', 'r') #文件打开模块 23 passwd = open(r'e:first weekpasswd.txt', 'r') 24 25 26 trid_name = 0 #这个模块来判断是否这个用户名尝试过很多次 27 with open(r'e:first week ecord_name.txt', 'r') as record_tried_name: 28 for i in range(num_record_username): 29 30 # record_buffer = record_tried_name.readline().rstrip() 31 if username == record_tried_name.readline().rstrip(): 32 print("This account have tried too many times!") 33 trid_name = 1 34 break 35 if trid_name == 1: 36 break 37 38 user_valid = 0 #用来标记用户名是否有效 39 40 j = 0 # 用来标记有效用户名的编号 41 42 for i in range(num_username): 43 username_buffer = user.readline().rstrip() #用来暂时存储用户名,可以不用 44 if username == username_buffer: 45 user_valid = 1 46 j = i # record the which username is right 47 break 48 49 if user_valid == 0: 50 print("Your account is not valid") 51 count += 1 52 if count >= 4: 53 print("Sorry you've tried too many times") 54 55 with open(r'e:first week ecord_name.txt', 'a') as record_tried_name: #该模块用来写入尝试过很多次的账户 56 record_tried_name.write("%s "%username) 57 58 num_record_username += 1 # 这个模块来记录记录的账户数量的变化 59 with open(r'e:first week ecord_num.txt', 'w') as change: 60 change.write("%s " % num_username) 61 change.write("%s " % num_record_username) 62 63 record_tried_name.close() 64 break #跳出while循环 65 66 else: 67 login_success = 0 68 for l in range(num_username): #l作为指针指示第几行 69 passwd_buffer = passwd.readline().rstrip() 70 if l == j: #如果l与登录用户的顺序一致,则进入之后的判断 71 if password == passwd_buffer: 72 print("yes,%s is in!"% username) 73 login_success = 1 74 break #跳出for循环 75 76 else: 77 print("Your password is wrong!") 78 count += 1 79 if count >=4: 80 print("Sorry you've tried too many times") 81 with open(r'e:first week ecord_name.txt', 'a') as record_tried_name: #用来写入尝试过很多次的账户 82 record_tried_name.write("%s " % username) 83 84 num_record_username +=1 #这个模块来记录记录的账户数量的变化 85 with open(r'e:first week ecord_num.txt', 'w') as change: 86 change.write("%s "%num_username) 87 change.write("%s "%num_record_username) 88 89 record_tried_name.close() 90 if login_success == 1: 91 break #跳出while循环,成功登录 92 user.close() #关文件 93 passwd.close() #关文件
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Ku
record_num = open(r'e:first week ecord_num.txt', 'r') # 用来记录用户密码以及尝试过的用户的数量
# 提取用户数量以及尝试过的用户名数量
num_username = int(record_num.readline().rstrip())
num_record_username = int(record_num.readline().rstrip())
#print(num_username,num_record_username)
record_num.close()
count = 1 #指示尝试的次数
while count <= 3:
username = input("Please enter your username:") #输入模块
#password = getpass.getpass("Please enter your password:")
password = input("Please enter your password:")
user = open(r'e:first weekusername.txt', 'r') #文件打开模块
passwd = open(r'e:first weekpasswd.txt', 'r')
trid_name = 0 #这个模块来判断是否这个用户名尝试过很多次
with open(r'e:first week ecord_name.txt', 'r') as record_tried_name:
for i in range(num_record_username):
# record_buffer = record_tried_name.readline().rstrip()
if username == record_tried_name.readline().rstrip():
print("This account have tried too many times!")
trid_name = 1
break
if trid_name == 1:
break
user_valid = 0 #用来标记用户名是否有效
j = 0 # 用来标记有效用户名的编号
for i in range(num_username):
username_buffer = user.readline().rstrip() #用来暂时存储用户名,可以不用
if username == username_buffer:
user_valid = 1
j = i # record the which username is right
break
if user_valid == 0:
print("Your account is not valid")
count += 1
if count >= 4:
print("Sorry you've tried too many times")
with open(r'e:first week ecord_name.txt', 'a') as record_tried_name: #该模块用来写入尝试过很多次的账户
record_tried_name.write("%s "%username)
num_record_username += 1 # 这个模块来记录记录的账户数量的变化
with open(r'e:first week ecord_num.txt', 'w') as change:
change.write("%s " % num_username)
change.write("%s " % num_record_username)
record_tried_name.close()
break #跳出while循环
else:
login_success = 0
for l in range(num_username): #l作为指针指示第几行
passwd_buffer = passwd.readline().rstrip()
if l == j: #如果l与登录用户的顺序一致,则进入之后的判断
if password == passwd_buffer:
print("yes,%s is in!"% username)
login_success = 1
break #跳出for循环
else:
print("Your password is wrong!")
count += 1
if count >=4:
print("Sorry you've tried too many times")
with open(r'e:first week ecord_name.txt', 'a') as record_tried_name: #用来写入尝试过很多次的账户
record_tried_name.write("%s " % username)
num_record_username +=1 #这个模块来记录记录的账户数量的变化
with open(r'e:first week ecord_num.txt', 'w') as change:
change.write("%s "%num_username)
change.write("%s "%num_record_username)
record_tried_name.close()
if login_success == 1:
break #跳出while循环,成功登录
user.close() #关文件
passwd.close() #关文件