0

Okay so im making an inventory command for my currency sys but im stuck with it. How should i exactly define that if user has nothing in his inv?

My code:

bag = users[str(user.id)]["bag"] # This is just the json where the items are stored.
for item in bag:
            name = item["item"]
            amount = item["amount"]

            if name in bag == None:
                em.add_field(name="Your bag is empty.")
            else:
                em.add_field(name=name,value=amount)

        await ctx.send(embed=em)

Traceback: No errors in console, it simply does not work.

jsxtdavee
  • 1
  • 3
  • you mean something like `if (name := item.get('item')) is None`? – timgeb Jan 18 '22 at 14:54
  • Would not an empty `bag` mean there's nothing in inventory? What's the type of `bag`? – outis Jan 18 '22 at 14:55
  • bag is the json, where the items are stored. bag = users[str(user.id)]["bag"] – jsxtdavee Jan 18 '22 at 14:56
  • @timgeb Yeah but that is the same as my code, no error just simply does not work. – jsxtdavee Jan 18 '22 at 14:58
  • 2
    JSON is a data interchange format, not a type. What type is `bag`? `dict`? `set`? `list`? Something else? [Sample code](//stackoverflow.com/help/mcve) should be complete. – outis Jan 18 '22 at 15:02
  • its a dict like this: {"822532757011300383": {"wallet": 321, "bank": 0}} – jsxtdavee Jan 18 '22 at 15:04
  • Does this answer your question? [Python: Checking if a 'Dictionary' is empty doesn't seem to work](//stackoverflow.com/q/23177439/90527) – outis Jan 18 '22 at 15:06
  • @outis I tried alll the suggestions there but it still hasnt solved my problem.. – jsxtdavee Jan 18 '22 at 17:29
  • Note the question is still unclear due to both the description and code being underspecified. Based on the answer, this question is a duplicate of "[Checking if a key in dict is defined or not](https://stackoverflow.com/q/26247933/90527)". Also, there's a code smell in that the item both comes from the bag and provides a key of something in the bag (which is the form of a linked list). – outis Jan 19 '22 at 20:58

1 Answers1

0

I found the solution.

The solution:

if not name in bag:

Explaining: "name in bag" returns a bool, and it will be never "None".

jsxtdavee
  • 1
  • 3