python的學習筆記01_6練習


# 一、【用戶登陸程序】
# 基礎需求:
# 讓用戶輸入用戶名密碼
# 認證成功后顯示歡迎信息
# 輸錯三次后退出程序

count = 0
name = "cheng"
password = "123456"
while count <3:
    count += 1
    your_name = input ("請輸入你的名字:")
    your_pwd = input ("請輸入你的密碼:")
    if your_name == name and your_pwd == password:
        print("登錄成功,歡迎進入..")
        break
    else:
        print("名字或密碼錯誤,你還有%s次機會" % (3-count))
View Code

# 升級需求:
# 可以支持多個用戶登錄 (提示,通過列表存多個賬戶信息)
# 用戶3次認證失敗后,退出程序,再次啟動程序嘗試登錄時,還是鎖定狀態(提示:需把用戶鎖定的狀態存到文件里)

lock = "lock"
account = "account"
flag = 1
count = 0
lock_user = []
file1 = open(lock, "r")
lock_file = file1.readline()
file1.close()

for i in lock_file:
    i = i.strip("\n")
    lock_user.append(i)

file2 = open(account, 'r')
account_file = file2.readlines()
file2.close()

while True:
    your_name = input("請輸入你的名字:")
    your_pwd = input("請輸入你的密碼:")
    if your_name in lock_file:
        print("你的用戶名已經給鎖定,請聯系管理員")
        break

    else:
        count += 1
        if count > 2:
            print("你的用戶名已經給鎖定,請聯系管理員")
            with open(lock, "a") as file:
                file.write(your_name + "\n")
            break

        else:
            for i in account_file:
                name, password = i.strip(" ").split()
                if your_name == name and your_pwd == password:
                    print("登錄成功,歡迎進入")
                    flag = True
                else:
                    continue
        if flag is True:
            break
View Code

 

# 二、【三級菜單】
# 數據結構:
#
# 需求:
# 可依次選擇進入各子菜單
# 可從任意一層往回退到上一層
# 可從任意一層退出程序
# 所需新知識點:列表、字典



menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '網易': {},
                'google': {}
            },
            '中關村': {
                '愛奇藝': {},
                '汽車之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龍觀': {},
        },
        '朝陽': {},
        '東城': {},
    },
    '上海': {
        '閔行': {
            "人民廣場": {
                '炸雞店': {}
            }
        },
        '閘北': {
            '火車站': {
                '攜程': {}
            }
        },
        '浦東': {},
    },
    '山東': {},
}

layer = [menu]
while layer:
    for i in layer[-1]:print(i)
    choice = input(">>:).strip()
    if choice in layer[-1].key() and layer[-1][choice]:
        layer.append(layer[-1][choice])
    elif choice == "b":layers.pop()
    elif choice.upper() =="Q": break
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM