刚刚学习在 Python 上编码,所以我创建了这个测试字典。我希望从字典中删除输入,但是每次在 pop 函数之后打印字典时,它都不会删除键或值。我使用的是 Python 3.7.2 ShellfirstDict = {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")
1 回答

幕布斯7119047
TA贡献1794条经验 获得超8个赞
您只需使用 将输入字符串转换为整数即可int()
。像这样:
inputKeyToDelete = int(input("Please enter a key whose corresponding value you wish to delete: "))
那是因为从标准输入input()
读取字符串。
添加回答
举报
0/150
提交
取消