Python初學——函數的應用之註冊登錄系統

要求模擬系統註冊及登陸的情景,註冊密碼要求:密碼是6位或以上,必須包含大寫字母、小寫字母、數字
程序未退出前,則一直循環進行

代碼如下:

import sys
import re

username = []
password = []


def main():
    print("*" * 20, "主界面", "*" * 20)
    print("1.用戶註冊")
    print("2.用戶登錄")
    print("3.退出")


def get_choice():
    a = int(input("請選擇輸入的序號:"))
    return a


def register():
    name = input("用戶名:")
    if len(name) > 0:
        username.append(name)
        pwd = input("密碼:")
        while 1:
            if re.match("^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).*$", pwd) == None:
                pwd = input("密碼中必須包含大寫數字、小寫字母和數字,且不少於6位,請重新輸入:")
            else:
                print("您的密碼有效,請牢記密碼!")
                password.append(pwd)
                print("賬號:", name, "密碼:", pwd)
                return False

    else:
        print("用戶名不能爲空,請重新輸入!")
        return register()


def login():
    input_name = input("請輸入用戶名:")
    while 1:
        if input_name in username:
            i = username.index(input_name)
            input_pwd = input("請輸入密碼:")
            if input_pwd == password[i]:
                print("登錄成功!")
                break
            else:
                print("密碼錯誤!")
        else:
            print("用戶名錯誤!")
            return login()


def exit_menu():
    print("----退出系統----")
    sys.exit()


i = 0
while i == 0:
    main()
    choice = get_choice()
    if choice == 1:
        register()
    elif choice == 2:
        login()
    elif choice == 3:
        i = exit_menu()
    else:
        print("輸入錯誤!")

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章