Python Forum
Python inventory system with dicts.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python inventory system with dicts.
#1
My problem involves the 'intro' variable. Adding stuff works just fine, but if I type in 'remove' for the 'intro' var, it goes to the part where i add stuff, instead of removing stuff.


inv = {"food": 
         {"apple": "amount",
          "orange": "amount",
          "watermelon": "amount",
          "grape": "amount",
          "pear": "amount",
          "banana": "amount",
          "tomato": "amount"},
       
        "electronics":
          {"televison": "amount",
           "desktop": "amount",
           "laptop": "amount",
           "printer": "amount",
           "cords": "amount",
           "phone": "amount"},
       
        "tools":
          {"hammer": "amount",
           "drill" : "amount",
           "screwdrive": "amount",
           "saw": "amount",
           "ax": "amount"},
       
        "furniture":
          {"couch": "amount",
           "chair": "amount",
           "table": "amount",
           "shelf": "amount"}}

intro = input("What would you like to do?: ")

if intro == "add" or "Add":
    where = input("Where would you like to add?: ")
    item = input("What would you like to add?: ")
    amount = input("How much would you like to add?: ")
    
    inv[where][item] = amount
    print(item + ": " + amount)

elif intro == "remove" or "Remove":
    where = input("Where would you like to remove?: ")
    item = input("What would you like to remove?: ")
    amount = input("How much would you like to remove?: ")
   
    inv[where][item] = item - amount


    
Reply
#2
You are using 'or' incorrectly. See this post.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ok, it worked, thank you for the help.

I also came across a other problem. When I try to remove items, it says:

Error:
Line 46: TypeError: unsupported operand type(s) for Sub: 'str' and 'str'
For example, I have ten apples, and I want to remove to, so then there are 8 apples, but the error message above comes up.
inv = {"food": 
         {"apple": 10,
          "orange": 0,
          "watermelon": 0,
          "grape": 0,
          "pear": 0,
          "banana": 0,
          "tomato": 0},
        
        "electronics":
          {"televison": 0,
           "desktop": 0,
           "laptop": 0,
           "printer": 0,
           "cords": 0,
           "phone": 0},
        
        "tools":
          {"hammer": 0,
           "drill" : 0,
           "screwdrive": 0,
           "saw": 0,
           "ax": 0},
        
        "furniture":
          {"couch": 0,
           "chair": 0,
           "table": 0,
           "shelf": 0}}
 
intro = input("What would you like to do?: ")
 
if intro == "add" or intro == "Add":
    where = input("Where would you like to add?: ")
    item = input("What would you like to add?: ")
    amount = input("How much would you like to add?: ")
     
    inv[where][item] = amount
    print(item + ": " + amount)
 
elif intro == "remove" or intro == "Remove":
    where = input("Where would you like to remove?: ")
    item = input("What would you like to remove?: ")
    amount = input("How much would you like to remove?: ")
    
    inv[where][item] = item - amount
Reply
#4
If you are running this in Python 3.x (which you should be), input returns strings. So you are storing the numbers as strings, not as integers. You need to convert the amounts to integers with the int() built-in:

>>> int('801')
801
>>> int('801') - 5
796
>>>'801' - 5  # causes an error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Ok, i got that part figured out, thanks, but im confused.. again. Apple already has 10, but when I go to add it, if I want to add 2, so it is 12, it sets it equal to 2. I also can't figure out the remove part at all.

intro = input("What would you like to do?: ")
 
if intro == "add" or intro == "Add":
    where = input("Where would you like to add?: ")
    item = input("What would you like to add?: ")
    amount = int(input("How much would you like to add?: "))
     
    inv[where][item] += amount
    print(item + ": " + str(amount))
 
elif intro == "remove" or intro == "Remove":
    where = input("Where would you like to remove?: ")
    item = input("What would you like to remove?: ")
    amount = int(input("How much would you like to remove?: "))
    
    inv[where][item] = item - int(amount)
    print(item + ": " + str(amount))
Reply
#6
It's working for me, and I think it's working for you, it's just that your output is misleading. Note that you are printing amount. That's what is added or subtracted, not the end result. The show the end result, you should print(inv[where][item]).

Also, if you want to do a case insensitive match, the typical way to do it is if intro.lower() == 'add':. The lower method converts the string to lowercase, so you just have to check against lowercase. Sorry, I should have mentioned that earlier.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I tried to figure out this one on my own for a while, but nothing I did worked. On the 'elif inventory' part I want to print out everything in a organized fashion. Like 'Apples: (amount)' and do that for every item. I would like some help please, if someone could lead me in the right direction I would appreciate it please, thank you.


inv = {"food": 
         {"apple": 0,
          "orange": 0,
          "watermelon": 0,
          "grape": 0,
          "pear": 0,
          "banana": 0,
          "tomato": 0},
        
        "electronics":
          {"televison": 0,
           "desktop": 0,
           "laptop": 0,
           "printer": 0,
           "cords": 0,
           "phone": 0},
        
        "tools":
          {"hammer": 0,
           "drill" : 0,
           "screwdrive": 0,
           "saw": 0,
           "ax": 0},
        
        "furniture":
          {"couch": 0,
           "chair": 0,
           "table": 0,
           "shelf": 0}}
 
while True:
    intro = input("What would you like to do?: ")


    if intro.lower() == "add":
        where = input("Where would you like to add?: ")
        item = input("What would you like to add?: ")
        amount = int(input("How much would you like to add?: "))

        inv[where][item] += amount
        print(item + ": " + str(inv[where][item]))

    elif intro.lower() == "remove":
        where = input("Where would you like to remove?: ")
        item = input("What would you like to remove?: ")
        amount = int(input("How much would you like to remove?: "))

        inv[where][item] -= amount
        print(item + ": " + str(inv[where][item]))

    elif intro.lower() == "inventory":
        print("hi")
        
    end = input("Would you like to quit?: ")
    if end == "yes":
            break
    elif end == "no":
        continue
Reply
#8
Well, you're going to want to loop over your dictionary if you want to print it all. The way I usually loop over a dictionary is with the items() method of the dictionary. That method returns a sequence of pairs of the keys of the dictionary and the values associated with those keys.

for category, items in inv.items():
    print(category, items)
If you do that, you will see it print tools (or maybe one of the other inv keys) and the dictionary with the different tools and their amounts. But really, you want to print individual tools and their amounts. So you want another loop, within the loop above, to loop over items in a similar way.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advice for Inventory System Extra 0 1,283 Feb-18-2022, 09:25 PM
Last Post: Extra
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,840 Jan-22-2021, 05:43 AM
Last Post: buran
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,495 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Communicating Roblox's Inventory API with python? 4TH4RV 1 2,029 Jun-22-2020, 10:30 AM
Last Post: snippsat
Question Difference between Python's os.system and Perl's system command Agile741 13 6,656 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Still working with dicts menator01 2 1,974 Nov-07-2019, 07:15 AM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,158 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  Merge dicts without override chisox721 4 3,181 Jul-20-2019, 01:45 AM
Last Post: chisox721
  Store a product/item in a inventory program viktoria_linn 1 4,027 Jul-02-2019, 09:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020