簡體   English   中英

python:從字典中提取信息

[英]python: Extracting info from a dictionary

自學如何編碼,我需要幫助

我正在嘗試確保用戶輸入正確的用戶名 (credentials_U) 和密碼 (credentials_P)

所需 OUTPUT

  • 如果他們都做對了,歡迎打印
  • 如果他們只獲得正確的用戶名,則會打印錯誤的密碼
  • 如果他們輸入錯誤的用戶名或密碼錯誤,則會打印不正確並再次循環。

當前 OUTPUT 不返回任何內容

directory = {"ash":"123",}

def logon():
credentials_U = input("please input your username: ")
credentials_P = input("please input you password: ")
access = False

while access != True:
    if credentials_U in directory:
        if credentials_P in directory:      #true and true
            print ("welcome")
            access = True
            break


    elif credentials_U in directory:
        if credentials_P not in directory:       #true and false
            print ("incorrect password")
            logon()


    elif credentials_U not in directory:
        if credentials_P in directory:       #false and true
            print ("incorrect")
            logon()


    elif credentials_U not in directory:
        if credentials_P not in directory:      #false and false
            print ("incorrect")
            logon()

    else:
        print("incorrect details")
        logon()

首先,您沒有正確訪問用戶輸入的字段的dict

其次,您使用遞歸而不是使用已有的循環。

這應該更好:

directory = {"ash":"123",}

def logon():
    access = False
    while access != True:

        credentials_U = input("please input your username: ")
        credentials_P = input("please input you password: ")
        if credentials_U in directory:
            if credentials_P == directory[credentials_U]:      #true and true
                print ("welcome")
                access = True
            else:
                print ("incorrect password")
        else:
            print("incorrect details")

您需要在所有地方進行更改,因為 credentials_U 將是密鑰,而 credentials_P 將是代碼下方的值檢查,您需要在整個文件中更改它

while access != True:
    if credentials_U in directory.keys():
        if credentials_P == dictionary[credentials_U]:      #true and true
            print ("welcome")
            access = True
            break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM