检查物品的库存
Check inventory for item
我一直在研究一个名为“游戏”的命令,但是对于这个命令,您的物品栏中需要一台平板电脑。总结一下,我的问题是:你如何让机器人检查物品(平板电脑)是否在你的库存中? 过去 3 小时我一直在努力解决这个问题,我曾多次尝试更改代码,例如将 JSON 文件更改为具有额外的独特稀有度,并检查该稀有度是否存在该项目,但这也没有任何效果。我没有错误,但也没有成功,当我 运行 命令没有任何反应,没有错误,服务器没有任何反应。如有任何帮助,我们将不胜感激!
命令:
@client.command()
async def game(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
try:
bag = users[str(user.id)]["bag"]
except:
bag = []
if bag.contains("tablet"):
await ctx.send("More coding. . .")
else:
await ctx.send("Buy a tablet!")
JSON 文件:
{"703287162640007278": {"wallet": 431, "bank": 534404, "bag": [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1}, {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1}, {"item": "pistol", "amount": 1}]}
如果我理解正确,bag
是字典列表,即您已经访问了 JSON 文件中的键 'bag'
:
"bag": [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1}, {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1}, {"item": "pistol", "amount": 1}]
您需要检查这些字典中是否有键 item
和值 tablet
。所以:
if any(element['item'] == 'tablet' for element in bag):
编辑:
尝试这样的事情:
bag = users[str(user.id)].get("bag")
print(bag)
# it should print the bag or None
# [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1},
# {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1},
# {"item": "pistol", "amount": 1}]
if bag:
if any(element['item'] == 'tablet' and element['amount'] > 0
for element in bag):
# do something if tablet in bag
else:
# no tablet in bag
else:
# do something is there is no bag
演示:
bag = [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1},
{"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1},
{"item": "pistol", "amount": 1}]
if bag:
if any(element['item'] == 'tablet' and element['amount'] > 0
for element in bag):
print('tablet found')
else:
print('no tablet in the bag')
else:
print('no bag')
输出:
tablet found
我一直在研究一个名为“游戏”的命令,但是对于这个命令,您的物品栏中需要一台平板电脑。总结一下,我的问题是:你如何让机器人检查物品(平板电脑)是否在你的库存中? 过去 3 小时我一直在努力解决这个问题,我曾多次尝试更改代码,例如将 JSON 文件更改为具有额外的独特稀有度,并检查该稀有度是否存在该项目,但这也没有任何效果。我没有错误,但也没有成功,当我 运行 命令没有任何反应,没有错误,服务器没有任何反应。如有任何帮助,我们将不胜感激!
命令:
@client.command()
async def game(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
try:
bag = users[str(user.id)]["bag"]
except:
bag = []
if bag.contains("tablet"):
await ctx.send("More coding. . .")
else:
await ctx.send("Buy a tablet!")
JSON 文件:
{"703287162640007278": {"wallet": 431, "bank": 534404, "bag": [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1}, {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1}, {"item": "pistol", "amount": 1}]}
如果我理解正确,bag
是字典列表,即您已经访问了 JSON 文件中的键 'bag'
:
"bag": [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1}, {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1}, {"item": "pistol", "amount": 1}]
您需要检查这些字典中是否有键 item
和值 tablet
。所以:
if any(element['item'] == 'tablet' for element in bag):
编辑: 尝试这样的事情:
bag = users[str(user.id)].get("bag")
print(bag)
# it should print the bag or None
# [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1},
# {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1},
# {"item": "pistol", "amount": 1}]
if bag:
if any(element['item'] == 'tablet' and element['amount'] > 0
for element in bag):
# do something if tablet in bag
else:
# no tablet in bag
else:
# do something is there is no bag
演示:
bag = [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1},
{"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1},
{"item": "pistol", "amount": 1}]
if bag:
if any(element['item'] == 'tablet' and element['amount'] > 0
for element in bag):
print('tablet found')
else:
print('no tablet in the bag')
else:
print('no bag')
输出:
tablet found