大家好,本文将围绕python编写一个简单的登录系统展开说明,python编写用户注册登录系统是一个很多人都想弄明白的事情,想搞清楚python简单登录注册程序编需要先了解以下几个事情。
1.注册账号
def sign_num():
# 1. 提示用户输入账号和密码
name = input('请输入账号:')
pw = input('请输入密码:')
# 2. 判断输入的账号是否已经注册过
# 1)获取之前已经注册过的所有的账号信息
try:
result = read_file('file1/account.txt')
all_account = eval(result)
except FileNotFoundError:
all_account = []
# 2)判断是否已经注册过
for x in all_account:
if x['name'] == name:
print('注册失败!该账号已经注册过!')
return
# 3. 完成注册
all_account.append({'name': name, 'pw': pw})
write_file('file1/account.txt', all_account)
print('注册成功!')
2.登录系统
def load_sts():
# 1. 提示用户输入账号和密码
name = input('请输入账号:')
pw = input('请输入密码:')
# 2. 判断是否能够登录成功
try:
result = read_file('file1/account.txt')
all_account = eval(result)
except FileNotFoundError:
print('登录失败!该账号没有注册!')
return
for x in all_account:
if x['name'] == name and x['pw'] == pw:
print('登录成功!')
return
print('登录失败!')
3.控制台界面显示
def read_file(path: str):
with open(path, 'r', encoding='utf-8') as f:
return f.read()
def write_file(path: str, data):
with open(path, 'w', encoding='utf-8') as f:
f.write(str(data))
while True:
print("===========================")
print()
print()
print("抠'1'注册账号")
print("抠'2'登录系统")
print("抠'3'退出系统")
print()
print()
print('===========================')
num = int(input('请输入你想办理的业务:'))
if num in (1, 2):
if num == 1:
sign_num()
else:
load_sts()
elif num == 3:
print('退出成功')
break