2020Python練習七——檔案處理2
@2020.3.15
周末綜合作業:
1、撰寫用戶登錄介面
#1、輸入賬號密碼完成驗證,驗證通過后輸出"登錄成功"
#2、可以登錄不同的用戶
#3、同一賬號輸錯三次鎖定,(提示:鎖定的用戶存入檔案中,這樣才能保證程式關閉后,該用戶仍然被鎖定)
username1 = input('請輸入你的名字:').strip() usercode1 = input('請輸入你的密碼:').strip() count=0 with open(r'D:\0tempt\db.txt',mode='rt',encoding='utf-8') as f: for line in f: #把用戶輸入的名字和密碼與讀出的內容作對比 username,usercode=line.strip('').split(':') if username1 == username and usercode1 == usercode: print('登錄成功') break else: print('賬號或密碼錯誤,請重試') count+=1 else: print('賬號或密碼輸錯三次,賬戶已被鎖定,請申請找回或修改密碼') with open(r'D:\0tempt\clockeduser.txt',mode='wt',encoding='utf-8') as f: f.write('{}:{}'.format(username1,usercode1))
2、撰寫程式實作用戶注冊后,可以登錄
提示:
while True:
msg = """
0 退出
1 登錄
2 注冊
"""
print(msg)
cmd = input('請輸入命令編號>>: ').strip()
if not cmd.isdigit():
print('必須輸入命令編號的數字,傻叉')
continue
if cmd == '0':
break
elif cmd == '1':
# 登錄功能代碼(附加:可以把之前的回圈嵌套,三次輸錯退出引入過來)
pass
elif cmd == '2':
# 注冊功能代碼
pass
else:
print('輸入的命令不存在')
while True:
msg = """
0 退出
1 登錄
2 注冊
"""
print(msg)
cmd = input('請輸入命令編號>>: ').strip()
if not cmd.isdigit():
print('必須輸入命令編號的數字,傻叉')
continue
if cmd == '0':
break
elif cmd == '1':
# 登錄功能代碼(附加:可以把之前的回圈嵌套,三次輸錯退出引入過來)
count=0
with open(r'D:\0tempt\db.txt',mode='rt',encoding='utf-8') as f:
for line in f: #把用戶輸入的名字和密碼與讀出的內容作對比
username,usercode=line.strip('').split(':')
if username1 == username and usercode1 == usercode:
print('登錄成功')
break
else:
print('賬號或密碼錯誤,請重試')
count+=1
else:
print('賬號或密碼輸錯三次,賬戶已被鎖定,請申請找回或修改密碼')
with open(r'D:\0tempt\clockeduser.txt',mode='wt',encoding='utf-8') as f:
f.write('{}:{}'.format(username1,usercode1))
elif cmd == '2':
# 注冊功能代碼
print("注冊賬號".center(40,"="))
info = {}
name = input("賬號名:").strip()
pwd = input("賬號密碼:").strip()
# 讀取檔案中已存在的賬號密碼資訊
with open("test1","r",encoding="utf-8") as f:
for line in f:
user_name, password = line.strip().split("-")
info[user_name] = password
else:
print('輸入的命令不存在')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/176254.html
標籤:Python
上一篇:python基礎day5dict
