Python之簡單的使用者登入和註冊,python使用者登入

來源:互聯網
上載者:User

Python之簡單的使用者登入和註冊,python使用者登入

#!/bin/bash/env python# -*- coding:utf-8 -*-def login(username,password):    """    用於使用者名稱密碼的登入    :param username: 使用者名稱    :param password:  密碼    :return:  True,使用者驗證成功;False,驗證失敗    """    with open('cai.log','r',encoding="utf-8") as f:        for line in f:      #一行一行的讀取            line = line.strip()     #去除兩端的空格及分行符號            line_list = line.split("$")     #以$符號進行分割            if username == line_list[0] and password == line_list[1]:                return True    return Falsedef register(username,password):    """    註冊使用者    :param username: 使用者名稱    :param password: 密碼    :return: True,註冊成功    """    with open('cai.log',"a",encoding="utf-8") as f:        temp = "\n" + username + "$" +password        f.write(temp)    return Truedef user_exist(username):    """    檢查使用者是否已經存在    :param username: 使用者名稱    :return: True,則使用者名稱已經存在;False,使用者名稱不存在    """    with open('cai.log',"r",encoding="utf-8") as f:        for line in f:            line = line.strip()            line_list = line.split("$")            if username == line_list[0]:                return True    return Falsedef main():    print("歡迎登陸瑞的系統")    inp = input("1: 登陸;2: 註冊")    user = input("請輸入使用者名稱:")    pwd = input("請輸入密碼:")    if inp == "1":        is_login = login(user,pwd)        if is_login:            print("登入成功")        else:            print("登入失敗")    elif inp == "2":        is_exist = user_exist(user)        if is_exist:            print("使用者已經存在,無法註冊")        else:            result = register(user,pwd)            if result:                print("註冊成功")            else:                print("註冊失敗")main()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.