为什么我的输入键没有从我的字典中删除?

Why isn't my input key being removed from my dictionary?

刚刚在 Python 上学习编码,所以我创建了这个测试词典。 我希望从字典中删除输入,但是每次我在 pop 函数之后打印字典时,它都不会删除键或值。 我用的是 Python 3.7.2 Shell

firstDict = {1:"five", 2:"ten", 3:"fifteen", 4:"twenty"}
print(firstDict)
inputKeyToDelete = input("Please enter a key whose corresponding value you wish to delete: ")
if inputKeyToDelete in firstDict:
    firstDict.pop(inputKeyToDelete)
print(firstDict)

如果我输入一个键(例如 2),我希望从字典中删除该键和值 (2:"ten") 我希望输出是: (1:"five", 3:"fifteen", 4:"twenty")

您只需使用 int() 将输入字符串转换为整数即可。 像这样:

inputKeyToDelete = int(input("Please enter a key whose corresponding value you wish to delete: "))

那是因为 input() 从标准输入读取 字符串