将整数添加到字典中(TypeError:'int'对象不可下标)

问题描述 投票:0回答:1

我正在尝试将用户输入的数字添加到字典中所选值的旁边。我需要这样做,因此许多用户可以将同一字典添加到计数中,但会收到此错误消息


inventory={'p': 0, 'd': 0, 'r': 0}

let = str(input("""From this selection:
1. p
2. d
3. r
Which letter would you like to produce?: """))
bottle_num = int(input("How Many numbers Would You Like?"))
for let in inventory:
    inventory[let] + bottle_num
print(inventory[let][0])

[产生]的输出>

From this selection:
1. p
2. d
3. r
Which letter would you like to produce?: r
How Many numbers Would You Like?5
Traceback (most recent call last):
  File "C:/Users/$$$$$$$$$$/add_to_dict.py", line 12, in <module>
    print(inventory[let][0])
TypeError: 'int' object is not subscriptable

在$中添加了$符号>

我正在尝试将用户输入的数字添加到字典中所选值的旁边。我需要这样做,因此许多用户可以将同一词典添加到计数中,但是我得到了...

python dictionary data-structures integer
1个回答
0
投票

您的问题是您访问词典的方式。注意以下内容:

inventory = {"p": 0, "d": 0, "r": 0}
let = "p"
print(inventory[let]) # ouputs => 0
© www.soinside.com 2019 - 2024. All rights reserved.