s14 python3 day1 作业2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了s14 python3 day1 作业2相关的知识,希望对你有一定的参考价值。

  需求:

模拟验证用户输入密码登录。要求有3次机会,如果用户只输错密码,则三次后锁定,下次再登陆则提示账号锁定。

如果用户名密码正确,则提示登陆成功。

 

做作业:

使用两个文本文件来分别存放用户信息和锁定信息

用户和密码列表

[[email protected] day1]# cat user_list.txt 
liuxiaolu 123456
liuxiaogou 888888
liuxiaomao 654321

黑名单列表

[[email protected] day1]# cat user_lock.txt

程序代码

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: leilei

# 定义变量
tries = 0 #尝试次数
tries_user = 0 #
account_flag = 0

while tries < 3:
    user_list = open(/dbdata/pythons/s14/day1/user_list.txt,r)
    user_lock = open(/dbdata/pythons/s14/day1/user_lock.txt, r)
    user_name = input("plz enter ur name:")
    user_pass = input("plz enter ur pass:")
    
    for _user_lock in user_lock.readlines():
        _user_block = _user_lock.strip().split()
        if user_name == _user_block[0]:
            print("this account has been locked!")
            account_flag = 1
            tries = 4
            break
    
    if account_flag == 0:
        for _user_name in user_list.readlines():
            _user = _user_name.strip().split()
            if user_name == _user[0] and user_pass == _user[1]:
                print("welcome to login...,",user_name)
                tries = 4
                break
            elif user_name == _user[0] and user_pass != _user[1]:
                print("wrong username or password!")
                tries_user = tries_user + 1
                break
        else:
            print("wrong username or password!")
        tries = tries + 1
        if tries < 4:
            print("u have",3-tries,"chances left!\n")
        if tries_user == 3:
            user_lock = open(/dbdata/pythons/s14/day1/user_lock.txt, a)
            user_lock.write(user_name+"\n")
            user_lock.close()
            print("lots of atempts, ur account has been locked.")

user_list.close()
user_lock.close()

测试

[[email protected] day1]# ./zuoye2.py   --测试账号锁定
plz enter ur name:liuxiaomao
plz enter ur pass:1
wrong username or password!
u have 2 chances left!

plz enter ur name:liuxiaomao
plz enter ur pass:2
wrong username or password!
u have 1 chances left!

plz enter ur name:liuxiaomao
plz enter ur pass:3
wrong username or password!
u have 0 chances left!

lots of atempts, ur account has been locked.  --提示被锁定
[[email protected] day1]# cat user_lock.txt 
liuxiaomao
[[email protected] day1]# ./zuoye2.py  --被锁定无法登陆
plz enter ur name:liuxiaomao
plz enter ur pass:11
this account has been locked!
[[email protected] day1]# ./zuoye2.py  --测试成功登陆
plz enter ur name:liuxiaolu
plz enter ur pass:123456
welcome to login..., liuxiaolu
[[email protected] day1]# ./zuoye2.py  --测试密码错误
plz enter ur name:liuxiao
plz enter ur pass:1
wrong username or password!
u have 2 chances left!

plz enter ur name:liuxiao
plz enter ur pass:2
wrong username or password!
u have 1 chances left!

plz enter ur name:liuxiao
plz enter ur pass:3
wrong username or password!
u have 0 chances left!

 




以上是关于s14 python3 day1 作业2的主要内容,如果未能解决你的问题,请参考以下文章

s14 第4天 关于python3.0编码 函数式编程 装饰器 列表生成式 生成器 内置方法

Python s12 Day1 笔记及作业

老男孩DAY1作业二

Python学习day1作业总结

Day1-作业2

day1:作业 编写登录接口并画出相应的流程图