python入门第一天作业。讲师写的代码。

#!/uer/bin/env python
# _*_ coding: utf-8 _*_

import sys
retry_limit = 3
retry_count = 0
account_file = 'accounts.txt'
lock_file = 'account_lock.txt'

while retry_count < retry_limit:                             #循环体限制3次内执行

    username = raw_input('33[32; Username : 33[0m')  #输入用户名
    lock_check = open(lock_file)                             #打开锁定文件
    for line in lock_check.readlines(): 
line = line.split() #循环锁定文件跟输入的用户名比对 if uesrname == line[0]: #判断用户输入的用户名和lock文件的用户名第一元素== sys.exit('31[31; User %s is locked!33[0m' %username) password = raw_input('33[32; Password : 33[0m') #输入pass f = file(account_file,'r+') match_flag = False for line in f.readlines(): #循环acc文件, user,password = lin.strip(' ').split() #把accounts文件拆分过滤 换行符,赋值给user,和pss变成列表 if username == user and password == password: #判断:输入的use等于acc里user 且 输入的pss等于列表里的pss print('Match!'),username match_flag = True break f.close() if match_flag == False: #判读标志位,默认不匹配。 #判断用户输入的user和pass是否与文件匹配,不配就继续循环,匹配上了就退出。 print 'User unmatched!' retry_count +=1 else : print('Welcom to in feifei blog system!') else: print("You account is locked!") f =file(lock_file,'ab') f.write(username) f.close()

作业:
编写登陆接口

输入用户名密码

认证成功后显示欢迎信息


输错三次后锁定

原文地址:https://www.cnblogs.com/feifei168/p/5775835.html