1.最基础的,用户名密码放到一个文件里,可以注册登陆查询注册用户名是否重复
def login(username, password): ''' 登录 :param username:用户输入的用户名 :param password: 用户输入的密码 :return:验证成功 ''' with open('userinfo.txt', 'r', encoding='utf-8') as oplogin: for line in oplogin: if line.split('%%')[0] == username and line.split('%%')[1] == password: return True return False def register(username, password): ''' 注册 :param username: 用户输入的用户名 :param password: 用户输入的密码 :return: 写入文件注册成功 ''' with open('userinfo.txt', 'a', encoding='utf-8') as opreg: opreg.write(' ' + username + '%%' + password) return print('注册成功') def same(username): ''' 查是否重复用户名 :param username: 用户输入用户名 :return:如果重复返回True,没有重复返回Flase ''' with open('userinfo.txt', 'r', encoding='utf-8') as opsame: for line in opsame: line = line.strip() if line.split('%%')[0] == username: return True return False # 这个return要在最外层,而不是在if语句里 def main(): choice = input('1:login,2:register') if choice == '1': username = input('username') password = input('password') if login(username, password): print('登录成功') else: print('登录失败') if choice == '2': username = input('username') password = input('password') if same(username): print('用户名已被占用') main() else: register(username, password) main()