码迷,mamicode.com
首页 > 其他好文 > 详细

老男孩DAY1作业二

时间:2018-01-13 23:38:14      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:author   dmi   lin   word   try   python   username   bing   obj   

作业内容:

(1)编写登录接口

(2)认证成功后显示欢迎信息

(3)输错三次锁定

 

以下为代码,分为两个部分的代码,一是register.py,另一个是login.py;其中register.py实现的注册功能,客户可输入用户名密码,用户名密码保存在txt文件中;login.py代码实现的是以下几个功能点:

(1)如果不存在该用户名,提示用户未注册;

(2)如果用户名在黑名单中,提示用户与管理员联系;

(3)如果密码输入正确,登录成功;如果密码输入错误,可以试三次,如果三次仍未成功,则将该用户名锁定,并记录到TXT中,下次再输入该用户名,则提示该用户已被锁定。

 

register.py

#!/usr/bin/env python
#Author:Bing

print("---------------")
print("If you want to be a member of our web, please register.")
print("---------------")

username = input("please Enter your username: ")
password = input("please Enter your password: ")

filename1 = ‘username.txt‘
with open(filename1, ‘a‘) as file_object:
    file_object.write(username)
    file_object.write("\n")

filename2 = ‘password.txt‘
with open(filename2, ‘a‘) as file_object:
    file_object.write(password)
    file_object.write("\n")

 

  login.py

#!/usr/bin/env python
#Author:Bing

usernames = []
passwords = []
usrpwddict = {}
count = 0
filename = ‘blacklist.txt‘

with open(‘username.txt‘) as file_object:
    for username_ in file_object.readlines():
        username_ = username_.strip(‘\n‘)
        usernames.append(username_)

with open(‘password.txt‘) as file_object:
    for password_ in file_object.readlines():
        password_ = password_.strip(‘\n‘)
        passwords.append(password_)

usrpwddict = dict(zip(usernames,passwords))

print(usernames)

print("---------------")
print("Login")
print("---------------")
usernamelist = []
usrpwd = []
username = input("Please enter your username: ")

if username not in usernames:
    print("You have not register yet! please register first!")
else:
    with open(‘blacklist.txt‘) as file_object:
        for usernames_ in file_object.readlines():
            usernames_ = usernames_.strip(‘\n‘)
            usernamelist.append(usernames_)
        for i in usernamelist:
            if username == i:
                print("Your Username is locked, Please contact the administrator!!!")
                break
        else:
            while count < 3:
                password = input("Please Enter your password: ")
                if usrpwddict[username] == password:
                    print("congratulation!")
                    break
                else:
                    count += 1
                    if count == 3:
                        print("You have tried too much times, now you are locked!")
                        with open(filename, ‘a‘) as file_object:
                            file_object.write(username)
                            file_object.write("\n")
                        break
                    print("try again!")

  

老男孩DAY1作业二

标签:author   dmi   lin   word   try   python   username   bing   obj   

原文地址:https://www.cnblogs.com/cumo/p/8280583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!