我已经使用python(程序是PyCharm)一段时间了,我不知道所有的python所以答案可能是盯着我看。
所以我有多个词典,大多数都包含在显示的同一个类中:
class Store(object):
storage = {
'Console CDs': 3,
'Computer CDs': 5,
'CD cases': 10,
'Game cartridges': 3,
'Cartridge Cases': 5,
'Xcubes': 0,
'GS1s': 0,
'CSs': 0
}
prices = {
'Console Game': 30.00,
'PC Game': 30.00,
'CS Game': 25.00,
'Xcubes': 250.00,
'GS1s': 250.00,
'CSs': 150.00
然后,我有一个在我制作的目录中购物的功能(其中目录也是类Store
中的字典。我坚持的部分是:
for key in Store.catalog:
if key == x:
z = input("How many?\n")
if profit >= float(z) * Store.catalog[key]:
y = input("Are you sure? This will cost you " + str(Store.catalog[key]) + " cash. Enter Yes or "
"No.\n")
if y.lower() == "yes":
profit -= float(z) * Store.catalog[key]
print("Current Money: $" + str(profit))
print("Purchase Successful.")
for item in Store.storage:
if x == item:
Store.storage[item].update({int(z)})
time.sleep(1)
main_function()
elif y.lower() == "no":
print("Purchase Canceled.")
time.sleep(1)
shopping()
精确定位for item in Store.storage loop
,如果用户成功购买商品,我想添加许多用户从目录购买的特定商品到他们的存储空间。
问题是,如果我把它写成
Store.storage[item].update({int(z)})
它没有检测到
.update
句法。但是,如果我这样做而没有定义类,
storage[item].update({int(z)})
,
它没有检测到storage
字典。
我的代码有问题,还是因为我在课堂上制作了词典?我应该删除课程吗?
答案 0 :(得分:2)
您要么:Store.storage.update({item:int(z)})
或者:Store.storage[item] = int(z)