1. 程式人生 > >s14 python3 day1 作業2

s14 python3 day1 作業2

如果 三次 嘗試 readline lose split() cnblogs 需求 程序代碼

  需求:

模擬驗證用戶輸入密碼登錄。要求有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