Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import pickle
- class Inventory():
- units = ["pounds", "units", "boxes"]
- def __init__(self):
- try:
- dbFile=open("Inventory.db","rb")
- self.invDict=pickle.load(dbFile)
- except:
- self.invDict = dict()
- print("No .db found, creating new database!")
- else:
- print("Opened Inventory.db!")
- for k,v in self.invDict.items():
- print("You have {} {} of {}!".format(v.count, v.unit, k))
- finally:
- input("Press enter to continue...")
- # self.invSet = set()
- # self.invList = list()
- def remItem(self):
- remove=True
- while remove==True:
- os.system("cls")
- print("Here is the current inventory, which selection would you like to remove from?")
- for k,v in self.invDict.items():
- print("{} {} of {}".format(v.count, v.unit, k))
- name=input()
- if name in self.invDict:
- print("Out of {} {}, how many {} would you like to remove?".format(self.invDict[name].count, self.invDict[name].unit, self.invDict[name].name))
- self.invDict[name].count - float(input())
- else:
- os.system("cls")
- choice = input("That's not currently in your inventory, would you like to add an item instead? (y/n)")
- if choice.lower() == "n" or choice.lower() == "N":
- remove=True
- else:
- Inventory.addItem()
- finally:
- os.system("cls")
- print("Now there are only {} {} of {}")
- choice = input("Would you like to remove another item? (y/n)")
- if choice.lower() == "n" or choice.lower() == "no":
- remove=False
- else:
- remove=True
- def addItem(self, name=None, count=None, unit=None):
- add = True
- while add == True:
- os.system("cls")
- print("What is the item name?")
- name = input()
- os.system("cls")
- if name in self.invDict:
- unit = self.invDict[name].unit
- print("You already have {} {} of {}, how many {} would you like to add?".format(self.invDict[name].count, self.invDict[name].unit, self.invDict[name].name, self.invDict[name].unit))
- while unit == None:
- try:
- print("What are the units?")
- for i in Inventory.units:
- print(i,Inventory.units.index(i))
- #inputUnit = int(input())
- #unit = Inventory.units[inputUnit]
- unit = Inventory.units[int(input())]
- except:
- os.system("cls")
- print("That unit is invalid, please type a number displayed:")
- print(i,Inventory.units.index(i))
- else:
- os.system("cls")
- print("You selected", unit+".")
- print("How many {} of {}?".format(unit, name))
- while count == None:
- try:
- count = float(input())
- except:
- print("Whoops, that's not a number! Please input a valid amount")
- else:
- os.system("cls")
- x = InvItem(name, count, unit)
- #print(x,x.name)
- #self.invDict[x.name]=x
- if x.name in self.invDict:
- self.invDict[x.name].count += x.count
- else:
- self.invDict[x.name] = x
- finally:
- pickle.dump(self.invDict, open("Inventory.db","wb"))
- for k,v in self.invDict.items():
- print("You have {} {} of {}!".format(v.count, v.unit, k))
- #print(self.invDict)
- choice = input("Would you like to add another item? (y/n)")
- if choice.lower() == "n" or choice.lower() == "no":
- add = False
- else:
- name=None
- count=None
- unit=None
- class InvItem(Inventory):
- def __init__(self, name, count, unit):
- super().__init__()
- self.name = name
- self.count = count
- self.unit = unit
- i = Inventory()
- i.addItem()
- input("Press enter to exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement