Week1作业: 登录小程序

     编写登录接口
     ->输入用户名和密码
     ->认证成功后显示欢迎信息
     ->输错三次后锁定

Week1作业: 登录小程序
import os

lock = False
fail_times = 0

uname = input("please input your name:")
passwd = input("input your password:")

#判断输入的用户是否锁定
if (os.path.exists(".\lock.txt")):
    f = open(".\lock.txt", "r")
    for line in f:
        userlockedinfo = line.split(":")
        if ((userlockedinfo[0] == uname) and ("locked" == userlockedinfo[1].strip())):
            lock = True
    f.close()


while (True):
    #从文件获取用户的密码
    if (os.path.exists("users.txt")):
        f1 = open("users.txt", "r")
        for line in f1:
            userinfo = line.split(":")
            if (userinfo[0] == uname):
                password = userinfo[1]
            else:
                print("user isn‘t exists!")
                exit(0)
    else:
        print("user data lost")
        exit()


    if (lock == True):
        print("your account already locked.")
        exit(0)

    #比较输入密码是否正确,输入三次错误后锁定
    if (passwd == password.strip()):
        print("welcome!", uname)
        fail_times = 0
        break
    elif(fail_times >= 3):
        if (fail_times >= 3):
            f2 = open(".\lock.txt", "w")
            f2.write(uname +":locked\n")
            f2.close()
            exit(0)
    else:
        fail_times = fail_times + 1
        uname = input("please try again, input your name:")
        passwd = input("input your password:")
View Code

 

Week1作业: 登录小程序

上一篇:最近做的几个小程序


下一篇:java取随机数