[英]Why this code can access the dictionary without “”?
在 Python 中,通過鍵入digit_mapping.get("1")
,我可以訪問此字典中的值 1。 我想知道為什么它仍然可以訪問字典而沒有在此代碼中的 get 方法中的字符周圍加上""
。
像output += digit_mapping.get("character", "!") + " "
phone = input("Phone: ")
digit_mapping = {
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four"
}
output = ""
for character in phone:
output += digit_mapping.get(character, "!") + " "
print(output)
正如評論中提到的, character
是一個變量。 它擁有一個價值; 在你的情況下,一個string
。 另一方面, "character"
是一個string
。 在您的示例中, "character"
將不返回任何內容,因為字典不帶有該名稱的鍵。 在另一方面,該值使可變character
包含( "1", "2", "3" and "4"
;對於內的每次迭代的值for
-loop)是一個有效的key
在字典中並且將產生字典中所需的value
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.