1. 讓使用者輸入使用者名稱密碼
2. 認證成功後顯示歡迎資訊、程式結束
3. 輸錯三次後退出程式
公升級需求:
#!/usr/bin/env python
# coding=utf-8
# __author__ = "zhaohongwei"
# date: 2019/1/27
name_list = ["zhangsan","lisi","wangwu"] # 使用者名稱列表
passwd_list = ["zhangsan666","lisi666","wangwu666"] # 使用者的密碼列表
count = [0, 0, 0] # 使用者登入密碼錯誤的次數計數
while true:
name_index = 999999 # 定義乙個不存在的使用者的下標
name = input("請輸入你的姓名:").strip()
passwd = input("請輸入你的密碼:").strip()
with open("lockedname.txt","r+") as f:
locked_name = "".join(f.readlines()).splitlines()
if name in locked_name:
continue
for i in range(len(name_list)):
if name == name_list[i]:
name_index = name_list.index(name_list[i])
if name_index == 999999:
print("使用者名稱不存在,請重新輸入")
continue
if name == name_list[name_index] and passwd == passwd_list[name_index]: # 判斷使用者名稱密碼
print("歡迎 %s 同學"%(name_list[name_index]))
break
else:
count[name_index] += 1 # 同乙個使用者名稱輸錯密碼加一
print("密碼錯誤")
if count[name_index] == 3 :
print("3次密碼錯誤,使用者賬號已被鎖定")
with open("lockedname.txt","a+") as f:
f.writelines(name_list[name_index]+"\n")
break
**二(修改版,用到了字典)
#!/usr/bin/env python# coding=utf-8
# __author__ = "zhaohongwei"
# date: 2019/1/27
import json
user = #使用者名稱:[密碼,密碼輸錯次數,false鎖定使用者]
while true:
name = input("請輸入你的姓名:").strip()
try:
with open("lockedname.txt","r+",encoding="utf-8") as f:
user = json.load(f) #若是有lockedname.txt檔案,則user重新賦值
except exception as err:
pass
if name not in user:
print("使用者名稱不存在,請重新輸入")
continue
elif user[name][2] == false:
continue
elif name in user:
passwd = input("請輸入你的密碼:").strip()
if passwd == user[name][0]: # 判斷使用者名稱密碼
print("歡迎 %s 同學"%(name))
user[name][1] = 0
with open("lockedname.txt", "w", encoding="utf-8") as f:
json.dump(user, f)
break
else:
user[name][1] += 1 # 同乙個使用者名稱輸錯密碼加一
print("密碼錯誤")
if user[name][1] >= 3:
print("使用者賬號已被鎖定")
user[name][2] = false
with open("lockedname.txt", "w", encoding="utf-8") as f:
json.dump(user, f)
編寫登陸認證程式 Python
作業1 編寫登陸認證程式 python 基礎需求 讓使用者輸入使用者名稱密碼 認證成功後顯示歡迎資訊 輸錯三次後退出程式 公升級需求 可以支援多個使用者登入 提示,通過列表存多個賬戶資訊 usr bin env python coding utf 8 uer name1 password name1...
簡單的登陸認證程式
編寫登陸認證程式 user info tom jerry f open black user user f.readlines lock user for i in user i i.strip print 鎖定使用者 lock user f.close count 0 count1 0 while...
Python學習之登陸認證
需求 讓使用者輸入使用者名稱密碼 認證成功後顯示歡迎資訊 輸錯三次後退出程式 可以支援多個使用者登入 提示,通過列表存多個賬戶資訊 如下 1 coding utf 8 2 date 3 12 2018 34 count 0 5 users 利用字典儲存多個使用者名稱 密碼 6 name input ...