I made myself a project which is to make a virtual inventory but I have been having difficulty with pickling the variable: inventory. Could someone please insert the strip of code in order to save the variable in a seperate file and make it able to import it.
import pickle
import time
def intro():
print("\t\t\tWelcome to inventory 1.0!")
print("\t\t Explore the wonders of the inventory.")
intro()
class inv(object):
amount=0
def update():
print("\nYou have:")
def add(inventory):
if invspace > len(inventory):
print("What would you like to add?")
inventory.append(input("Item: "))
print("You have:")
for item in inventory:
print(item)
time.sleep(0.2)
print("\n\n")
else:
print("You dont have enough space!")
def remove(inventory):
dec=None
rit=None
end=int(len(inventory))
print (end)
if len(inventory) > 0:
print("What item would you like to remove?")
while dec not in range(0,end):
try:
dec=int(input("Item. "))-1
if dec not in range(0,end):
if dec > 6:
print("You only have 8 inventory slots.")
else:
print("You dont have an item",dec+1,".")
except ValueError:
print("You must type a number.")
rit=inventory[dec]
inventory.remove(inventory[dec])
print("The item",rit,"has been removed.\n\n")
else:
print("You do not have any items in your inventory!\n\n")
def empty(inventory):
if len(inventory) > 0:
print("Are you sure you would like to empty your inventory?")
dec=None
while not dec in ("yes","no"):
dec=input("<yes/no>: ")
if dec==("yes"):
inventory=[]
print("Your inventory has been emptied.\n\n")
return inventory
else:
print("You do not have any items in your inventory!\n\n")
def visrep(inventory):
print(" --- --- --- ")
print("| | | |")
print(" --- --- --- ")
print(" --- --- --- ")
print("| | | |")
print(" --- --- --- ")
print(" --- --- --- ")
print("| | | |")
print(" --- --- --- \n\n")
def save(inventory):
inventory=inventory
print("Pickle here")
invspace=5
print("import pickle here")
inventory=[]
con=1
def decl():
print("1 - Add item |")
print("2 - Remove item |")
print("3 - Empty inventory |")
print("4 - Visual representation |")
print("5 - Save")
print("6 - Quit |")
print("--------------------------\n\n")
while con==1:
print("What would you like to do?")
decl()
dec=None
while not dec in range(1,7):
try:
dec=int(input("<1-6>: "))
if not dec in range(1,7):
print("Enter a number between 1 and 6")
except ValueError:
print("Enter a number between 1 and 6")
if dec==1:
inv.add(inventory)
if dec==2:
inv.remove(inventory)
if dec==3:
inv.empty(inventory)
if dec==4:
inv.visrep(inventory)
if dec==5:
inv.save(inventory)
if dec==6:
import sys
raise SystemExit