本文執行個體為大家分享了python實作登入與注冊系統的具體代碼,供大家參考,具體内容如下
實作功能
1.調用文本檔案裡的使用者資訊
2.可以将注冊資訊存儲在文本檔案裡
3.實作了密碼格式的限制
具體使用者資訊将如下格式存儲在txt文本檔案下

轉換後便于代碼利用的格式(清單中嵌套字典)
具體代碼如下:
#-*- coding=utf8 -*-
# @author:sololi
# date: 2020/11/3
# 檔案說明 :
import sys
def register(username,password):#登入功能,且與存儲使用者表的文本檔案進行比較
#驗證使用者名
shuju=readfile()
jg1 = 0
i = 0
while (i < len(shuju)):
if (username == shuju[i]["使用者名"]):
print("使用者名正确")
jg1 = 1
break
i += 1
# 使用者名錯誤将不再驗證密碼
if (jg1 != 1):
print("使用者名錯誤")
# 驗證密碼
if (jg1 == 1):
jg2 = 0
i = 0
while (i < len(shuju)):
if (password == shuju[i]["密碼"]):
print("密碼正确")
jg2 = 1
break
i += 1
if (jg2 != 1):
print("密碼錯誤")
def logon(username):#注冊功能,且以正确格式存入文本檔案
shuju=readfile()
jg3 = 0
i = 0
while (i < len(shuju)):
if (username == shuju[i]["使用者名"]):
print("使用者名已經存在")
jg3 = 1
break
i += 1
if(jg3 == 0):
while True:
password = input("請輸入注冊的密碼(密碼不能小于6位,且不能為純數字)")
if (str.isdigit(password)==1) or (len(password)<6):
print("密碼格式錯誤")
else:
break
passwordagain=input("請再次确認密碼")
while True:
if(password==passwordagain):
break
else:
print("兩次密碼不一緻")
passwordagain = input("請再次确認密碼")
# 将注冊的使用者資訊存儲到文本檔案中
f = open("data", mode='a+', encoding="utf8")
if shuju == []:
f.write("使用者名:{},密碼:{}".format(username, password))
if shuju != []:
f.write("\n使用者名:{},密碼:{}".format(username, password))
print("注冊成功")
f.close()
def readfile():#将資料轉換成清單字典形式,放在data.txt中便于後面登入與注冊存放資料
f = open('data', "r+", encoding="utf8")
shuju = []
b = []
aa = {}
for line in f.readlines():
line = line.strip('\n')
a = line.split(' ')
i = 0
while i < len(a):
b = a[i].split(',')
i += 1
j = 0
while j < len(b):
if b == " ":
break
c = b[j].split(':', 1)
aa[c[0]] = c[1]
i += 1
j += 1
shuju.append(aa.copy()) # copy是為了防止添加是資料類型不同出錯
f.close()
return shuju
while True:
choice=input("登入輸入1,注冊輸入2,其他任意鍵退出")
if choice=="1":
id=input("輸入您的賬号")
pw=input("輸入您的密碼")
register(id,pw)
break
if choice=="2":
id=input("輸入你注冊的賬号")
logon(id)
continue
else:
print("退出成功")
sys.exit(0)
初學者作品,有錯請指教
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援腳本之家。