Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查物品的库存_Python_Python 3.x_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python 检查物品的库存

Python 检查物品的库存,python,python-3.x,discord.py,discord.py-rewrite,Python,Python 3.x,Discord.py,Discord.py Rewrite,我一直在开发一个名为“game”的命令,但是对于这个命令,您的库存中需要一个平板电脑。总而言之,我的问题是:你如何让机器人检查物品(平板电脑)是否在你的库存中?在过去的3个小时里,我一直在努力解决这个问题,我多次尝试更改代码,比如将JSON文件更改为具有额外的唯一稀有性,并检查物品是否存在,然而,这也没有任何效果。我没有收到任何错误,但也没有成功,当我运行命令时,服务器中什么也没有发生,没有错误,什么也没有发生。任何帮助都将不胜感激 命令: @client.command() async def

我一直在开发一个名为“game”的命令,但是对于这个命令,您的库存中需要一个平板电脑。总而言之,我的问题是:你如何让机器人检查物品(平板电脑)是否在你的库存中?在过去的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": [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1}, {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1}, {"item": "pistol", "amount": 1}]

如果我理解正确,
bag
是dict列表,即您已经从JSON文件访问了key
'bag'

{"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": [{"item": "milk", "amount": 2}, {"item": "Golden Coin", "amount": 1}, {"item": "alarm", "amount": 1}, {"item": "tablet", "amount": 1}, {"item": "pistol", "amount": 1}]
您需要检查这些DICT中是否有键
和值
平板电脑
。因此:

如果有(元素['item']=='tablet'用于袋子中的元素):

编辑: 试着这样做:

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
演示:

输出:

tablet found

非常感谢。这回答了主要问题,但是仍然存在问题。如果'amount'=0,则项目仍然存在,但您没有购买。但再次感谢您提出的主要问题!我现在将努力解决数量问题:)只需在条件中添加
和元素['amount']>0
,也可以将其重构为简单的dict,例如
'bag':{'milk;:2,'tablet':1…}
。它将极大地简化访问。是的,我提出了相同的解决方案,但是现在它不再给我错误,也不再给我服务器输出。我很困惑,因为代码看起来不错,删除空白尝试,除了检查你得到的袋子。