要求:使用python制作一个简单的登录系统,使之可以访问数据文件(json格式),读取数据文件中的登录用户名信息,并提供注册功能,在其登录时进行账户验证。
本题的难点在于对于json文件的操作,我们知道对于json文件,正常的字典或列表无法直接写入,因此,首先将其序列化:
json.dumps(data1)
同理在读取json文件时,也要将其反序列化:
json.load(file1)
在解决读写文件问题后,然后就要完成注册部分:
def creat():
name = input("insert your Username: ")
password = input("insert your password: ")
if password.isalnum() == False:
print("unable password")
input("press enter to insert again")
data1={}
db[name]=password
with open("sql.json",'r',encoding='utf') as file1:
data1=json.load(file1)
file1.close()
# print(data1)
# print(type(data1))
print("successfully created")
file1.close()
data1[name] = password
with open("sql.json",'w',encoding='utf') as file2:
file2.write(json.dumps(data1))
file2.close()
由于使用的json文件,无法通过设置读写模式"a"的方式来实现注册一个写入一个,因此,我们呵采用文件覆盖的方式,对json文件覆盖,从而实现数据文件的更新。
然后,再来完成登录验证的功能。
本部分功能实现并不难,读取json文件后,将其数据转换为字典类型,在每读取一个用户名后,先检查数据是否包含,若包含,则可继续输入密码。此外还提供密码输错3次以上封号的功能。
def load():
while True:
username=input("insert your username: ")
if username in db:
password=input("insert your password: ")
if password == db[username]:
print("successfully logged in")
return 1
else:
print("error in password")
i=0
while i<2:
print("try again")
password=input("insert your password: ")
i+=1
if db[username] == password:
print("successfully logged in")
break
else:
continue
if i == 2:
db[username]='000000'
print("账户已初始化(封禁)")
return 0
else:
print("user not found")
break
最后,只需要加上主要调用部分即可。
if __name__ == "__main__":
logl()
while True:
m=input("enter your choice (1 to create,2 to log in,3 to exit): ")
if m =="1":
n=input("enter how many accounts you want to create: ")
if m=="1":
for i in range(int(n)):
creat()
elif m=="2":
print(db)
jj =load()
if jj ==1:
index()
break
elif m=="3":
break
、
全代码如下:
import json
def creat():
name = input("insert your Username: ")
password = input("insert your password: ")
if password.isalnum() == False:
print("unable password")
input("press enter to insert again")
data1={}
db[name]=password
with open("sql.json",'r',encoding='utf') as file1:
data1=json.load(file1)
file1.close()
# print(data1)
# print(type(data1))
print("successfully created")
file1.close()
data1[name] = password
with open("sql.json",'w',encoding='utf') as file2:
file2.write(json.dumps(data1))
file2.close()
db={}
def logl():
with open("sql.json",'r') as file:
data=json.load(file)
global db
db=data
print(db)
print(type(db))
file.close()
def load():
while True:
username=input("insert your username: ")
if username in db:
password=input("insert your password: ")
if password == db[username]:
print("successfully logged in")
return 1
else:
print("error in password")
i=0
while i<2:
print("try again")
password=input("insert your password: ")
i+=1
if db[username] == password:
print("successfully logged in")
break
else:
continue
if i == 2:
db[username]='000000'
print("账户已初始化(封禁)")
return 0
else:
print("user not found")
break
def index():
print("welcome to log in")
if __name__ == "__main__":
logl()
while True:
m=input("enter your choice (1 to create,2 to log in,3 to exit): ")
if m =="1":
n=input("enter how many accounts you want to create: ")
if m=="1":
for i in range(int(n)):
creat()
elif m=="2":
print(db)
jj =load()
if jj ==1:
index()
break
elif m=="3":
break