# 登录功能
def login():
import os
while True:
login_username = input('登录==> 用户[退出登录程序q]: ').strip()
if login_username == 'q':
print('''
已退出登录程序!!!!
''')
break
if not os.path.exists('db.txt'):
with open('db.txt', 'w', encoding='utf-8') as _:
...
with open('db.txt','r',encoding='utf-8',) as f:
for line in f:
username,password = line.strip().split(':')
if login_username == username:
while True:
login_password = input('登录==>密码: ').strip()
if login_password == password:
print(f'{login_username} 登录成功!\n')
break
else:
print('密码错误,请重新输入!!!\n')
break
else:
print(f'{login_username} 用户不存在,请重新输入!!!')

# 注册功能
def register():
import os
while True:
register_username = input('注册==> 用户[退出注册程序q]: ').strip()
if register_username == 'q':
print('''
已退出注册程序!!!!
''')
break
register_password = input('注册==> 密码: ').strip()
if len(register_username) == 0 or len(register_password) == 0:
print('非法输入,不能为空!')
continue
if not os.path.exists('db.txt'):
with open('db.txt','w',encoding='utf-8') as _:
...
with open('db.txt','r+',encoding='utf-8') as f:
for line in f:
a1,_ = line.split(':')
if a1 in register_username:
print('该用户已存在,请重新注册!')
break
else:
f.seek(0,2)
f.write(f'{register_username}:{register_password}\n')
print(f'[{register_username}] 注册成功!\n')

# 功能字典
dict_list = {
'0':('退出',None),
'1':('登录',login),
'2':('注册',register),
}

# 功能调用
while True:
for line in dict_list:
print('==>',line,dict_list[line][0])
number = input('请输入功能编号: ')
if number == '0':
print('退出程序!')
break
print('请输入编号,大憨憨!\n') if not number.isdigit() else ...
print('对不起, 该编号不存在\n') if not dict_list.get(number) else dict_list[number][1]()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.

ps:这是函数利用,当然也有三层架构的方式,不过这些功能完全用不到

#加油干,撸起袖子进步进步!!!