这是代码:
dict1 = {"games" : ["football", "cricket"]}
print(dict1)
input1 = input("enter key : ")
input2 = input("enter value : ")
dict1[input1].pop(input2)
它给出的输出为:
'games': ['football', 'cricket']}
enter key : games
enter value : football
Traceback (most recent call last):
File "C:/Users/fateo/PycharmProjects/pythonTuts/10Dictionary.py", line 116, in <module>
dict1[input1].pop(input2)
TypeError: 'str' object cannot be interpreted as an integer
Process finished with exit code 1
它与附加一起工作正常
dict1[input1].append(input2)
即使我尝试使用 for 循环:
for key, values in dict1.items():
values.pop(input2)
它给出的错误为:
{'games': ['football', 'cricket']}
enter key : games
enter value : football
Traceback (most recent call last):
File "C:/Users/fateo/PycharmProjects/pythonTuts/10Dictionary.py", line 113, in <module>
values.pop(input2)
TypeError: 'str' object cannot be interpreted as an integer
Process finished with exit code 1
当我使用 (int) 时:
input2 = int(input("enter value : "))
它给出的错误为
Traceback (most recent call last):
File "C:/Users/fateo/PycharmProjects/pythonTuts/10Dictionary.py", line 110, in <module>
input2 = int(input("enter value : "))
ValueError: invalid literal for int() with base 10: 'football'
我也用了del
del dict1[input2]
它说
TypeError: 'str' object cannot be interpreted as an integer
我不明白为什么它把它解释为整数
Smart猫小萌
猛跑小猪
慕莱坞森
相关分类