Python Forum

Full Version: Python simple store inventory system.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What I am trying to do is make a SIMPLE python inventory system but I am having trouble. Just so you know its not done yet. In the "if" statement part I want the user to choose which list to add a item to, but I don't want to do a "if" statement for each list, so I'm trying to find a way to where it just adds to the list that you type in to make it easier but I get a error message. I get a error though.

Error:
Line 14: AttributeError: 'str' object has no attribute 'append'
print("Welcome to Inventory Managment, type 'help' if you need help"

food = []
tools = []
electronics = []
books = []
furniture = []

command = input("What would you like to do?: ")
if command == "add":
	cato = input("Where would you like to add?: ")
	item = input("What item would you like to add?: ")
	
	cato.append(item)
print(food)
cato is a string, you have used the wrong variable.
I guess you want to append to food.
The way to do it is with a dictionary: inventory = {'food': [], 'tools': [], ...}. By using strings for the keys, this allows you to use the user's input as a key: inventory[cato].append(item).

You may want to look at the collections module, specifically defaultdict. It provides an easy way to do this.
(Aug-19-2018, 04:51 PM)2skywalkers Wrote: [ -> ]What I am trying to do is make a SIMPLE python inventory system
You will soon need to store persistent data on disk. I suggest using module tinydb for this. You can write simple things such as
db.insert({'item': 'Wuthering Heights', 'shelf': 'books'})