大家好,小编来为大家解答以下问题,python编写一个简单的登录系统,python制作一个登录注册界面,现在让我们一起来看看吧!
Source code download: 本文相关源码
import sys
import hashlib
users = []
slat = "JiNiTaiMei@@@@@@ChenZiHan!!!"
def password_md5(password) :
md5 = hashlib.md5(password.encode("utf-8"))
md5.update(slat.encode("utf-8"))
return md5.hexdigest()
def main() :
print("~*"*20)
print("\t\t 1.用户注册")
print("\t\t 2.用户登录")
print("\t\t 3.退出系统")
print("~*"*20)
choice = input("请输入您要操作的选项:")
return choice
def register():
username = input("请输入您的用户名称:")
password = input("请输入您的用户密码:")
if username == None or username.strip()=="":
print("用户名不能为空")
return
if exits_user(username):
print("对不起,该用户已经存在,请重新输入")
return
user = {}
user["username"]=username
user["password"]=password_md5(password)
users.append(user)
print(users)
def exits_user(username):
for i in users:
if i.get("username")==username:
return True
return False
def is_login(username,password):
for i in users:
if i.get("username")==username and i.get("password")==password:
print("登录成功")
return True
return False
def login():
username=input("输入用户名:")
password=input("输入用户密码:")
password=password_md5(password)
if is_login(username,password):
print("恭喜你登录成功")
else:
print("对不起,登录失败,请重新登录")
while True:
choice=main()
if choice == "1":
print("用户注册")
register()
elif choice=="2":
print("用户登录")
login()
elif choice=="3":
print("程序正常退出")
sys.exit()
else:
print("请输入正确的数字")
main()
运行结果: