簡體   English   中英

從字典中打印值

[英]Printing values from dictionary

我在從我制作的字典中打印值時遇到問題。 字典包含一個詞作為鍵和對該詞的描述作為值。 我遇到的問題是在 function 中,它應該打印用戶輸入的單詞(查找)的描述,但它沒有按我的意願工作。

我已經實現了一個 for 循環來搜索用戶想要在字典中查找的單詞,然后打印該單詞的描述(值)。 它有點工作。 問題是,例如,如果字典中有一個詞:banana and apple 和描述:yellow and fruit。 如果我想查找“蘋果”,它將起作用。 然后它將打印“蘋果的描述:水果”。

如果我想查找“香蕉”的描述,就會出現問題。 因為它是一個循環(我猜單詞的最新值是蘋果),它首先會通過 go 並打印“字典中沒有這個詞:”,然后打印“香蕉的描述。黃色”。 所以我想解決這個問題,我應該實現另一種找到密鑰的方法,而不是循環。 我只是不知道怎么做。

def dictionary():
    dictionary = {}
    while True:
        print("\n--- Menu for dictionary ---\n Choose 1 to insert a new word\n Choose 2 to find the description of a word\n Choose 3 to exit\n")
        answer = input("Type your answer here: ")
        if answer == "1":
            insert(dictionary)
        elif answer == "2":
            lookup(dictionary)
        elif answer == "3":
            break
        else:
            print("\nTry again!\n")

def insert(dictionary):
    word = input("Type the word you want to add: ")
    description = input("Type a description of the word: ")
    if word in dictionary:
        print("\nThe word already exists in the dictionary!\n")
        return
    else:
        dictionary[word] = description

def lookup(dictionary):
    wordsearch = input("What word would you like to lookup: ")
    for word, description in ordlista.items():
        if word == wordsearch:
            print("\nDescription of", word,":", description)
            break
        else:
            print("The word is not in the dictionary!")

您只需要刪除 else 條件並確保僅在循環結束時打印第二條語句(並且永遠不會遇到中斷部分)。

使用當前版本的代碼,對於循環的每次迭代都會執行條件,因此如果失敗,它只會打印“未找到”。

這是一個例子:

def dictionary():
    dictionary = {}
    while True:
        print("\n--- Menu for dictionary ---\n Choose 1 to insert a new word\n Choose 2 to find the description of a word\n Choose 3 to exit\n")
        answer = input("Type your answer here: ")
        if answer == "1":
            insert(dictionary)
        elif answer == "2":
            lookup(dictionary)
        elif answer == "3":
            break
        else:
            print("\nTry again!\n")

def insert(dictionary):
    word = input("Type the word you want to add: ")
    description = input("Type a description of the word: ")
    if word in dictionary:
        print("\nThe word already exists in the dictionary!\n")
        return
    else:
        dictionary[word] = description

def lookup(dictionary):
    wordsearch = input("What word would you like to lookup: ")
    for word, description in dictionary.items():
        if word == wordsearch:
            print("\nDescription of", word,":", description)
            break
    print("The word is not in the dictionary!")
            
            
dictionary()

我還想補充一點,如果你的字典很大,遍歷字典中的所有值可能不是很有效。 出於這個原因,我還想建議使用 get() 的另一種解決方案:

def dictionary():
    dictionary = {}
    while True:
        print("\n--- Menu for dictionary ---\n Choose 1 to insert a new word\n Choose 2 to find the description of a word\n Choose 3 to exit\n")
        answer = input("Type your answer here: ")
        if answer == "1":
            insert(dictionary)
        elif answer == "2":
            lookup(dictionary)
        elif answer == "3":
            break
        else:
            print("\nTry again!\n")

def insert(dictionary):
    word = input("Type the word you want to add: ")
    description = input("Type a description of the word: ")
    if word in dictionary:
        print("\nThe word already exists in the dictionary!\n")
        return
    else:
        dictionary[word] = description

def lookup(dictionary):
    wordsearch = input("What word would you like to lookup: ")
    if dictionary.get(wordsearch):
        print("\nDescription of", wordsearch,":", dictionary.get(wordsearch))
    else:
        print("The word is not in the dictionary!")
            
            
dictionary()

以下是我使用的 Get 方法的一些基本信息:

獲取參數

get() 參數 get() 方法最多接受兩個參數:

key - 要在字典中搜索的鍵 value (可選) - 如果未找到該鍵則返回的值。 默認值為無。 get() get() 方法的返回值返回:

如果鍵在字典中,則為指定鍵的值。 如果未找到鍵且未指定值,則為無。 如果未找到鍵並且指定了值,則為值。

獲得回報

get() get() 方法的返回值返回:

如果鍵在字典中,則為指定鍵的值。 如果未找到鍵且未指定值,則為無。 如果未找到鍵並且指定了值,則為值。

有關 Python 字典的 get() 方法的更多信息,請點擊此處

諾里說得對——這里有更多細節。 您的 function:

def lookup(dictionary):
    wordsearch = input("What word would you like to lookup: ")
    for word, description in ordlista.items():
        if word == wordsearch:
            print("\nDescription of", word,":", description)
            break
        else:
            print("The word is not in the dictionary!")

可以重寫為使用 get 而不是遍歷鍵:

def lookup(dictionary):
    wordsearch = input("What word would you like to lookup: ")
    look_result = ordlista.get(wordsearch)
    print(f"\nDecription of {word}: {look_result}" if look_result else "\nThe word is not in the dictionary!")

暫無
暫無

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

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