python-login

需求:输入用户名,密码,登陆网站,用户名密码保存在account文件中,如果输入密码错误三次,则账户被锁,被锁的账户存在lock文件中。

练习重点:文件处理,split,strip

#! /usr/bin/env python
# coding=utf-8
account_file = 'account.txt'
lock_file = 'lock.txt'

f = open(account_file,"r")
account_list = f.readlines()  #readline和readlines的区别:readline得到文件的第一行,而readlines是得到一个list,每一个索引值是每一行的数据
f.close()
print account_list

while True:
    f = open(lock_file)
    lock_list = []
    for i in f.readlines():
        line = i.strip("\n")
        lock_list.append(line)
    f.close()
    flag = False
    username = raw_input("Please input your name:").strip()
    if username in lock_list:
        print "your name is in lock_list,you can not login!!"
        break
    for line in account_list:
        line = line.split()   #line得到account_list中的第index个数据,是用户名空格密码的形式,我们需要拿到用户名,所以使用split分割字符串得到一个list
        if username == line[0]:
            for i in range(3):
                password = raw_input("Please input your password:").strip()
                if password == line[1]:
                    print "Welcome,you login the web!!enjoy~~"
                    flag = True
                    break
            else:
                f = open(lock_file, "a")
                f.write(username+"\n")
                f.close()
                print "Entered three times wrong password!going to lock %s" %username
            if flag is True:
                break
    if flag is True:
        break

 

posted on 2016-11-30 17:39  初出茅庐的孩纸  阅读(200)  评论(0)    收藏  举报

导航