Home > Blockchain >  Why is the dictionary not updating outside the definition even if i've returned the dictionary?
Why is the dictionary not updating outside the definition even if i've returned the dictionary?

Time:11-11

So the this dictionary is basically from a txt file and I formatted in order to have the same format as its supposed to have in order for my other definitions to work. The problem I'm having is that the for the loadInventory function, dictionary works inside the function but outside even though I've returned the dictionary the value doesn't return. The other definitions had no problem returning the dictionary or its values.

def addProduct(dictionary):
    prod_id = input("Enter prod ID: ")
    prod_name = input("Enter prod name: ")
    prod_input = input("Enter prod desc: ")
    prod_quantity = int(input("Enter prod quality: "))

    dictionary[prod_id] = [prod_name,prod_input,prod_quantity]


    return dictionary


def saveInventory(dictionary):

    save = open('inventory.dat', 'w')

    for key, value in dictionary.items():
        save.write(key   '\n')
        save.write(value[0]   '\n')
        save.write(value[1]   '\n')
        save.write(str(value[2])   '\n')


    save.close()

    return dictionary


def loadInventory(dictionary):

    dicty = {}
    n = 4


    with open('inventory.dat','r') as file:
        your_list = [line.strip() for line in file.readlines()]

    final = [your_list[i * n:(i   1) * n] for i in range((len(your_list)   n - 1) // n )]

    dictionary= dict((value[0], value[1:]) for value in final)
    
    print(dictionary)

    return dictionary


dictionary= {}
prodID = ""

#the actual code for the main menu

loop = True
while loop:
    menu()
    menuoption = int(input("Enter Choice: "))

    if menuoption == 1:
        addProduct(dictionary)       

    elif menuoption == 7:
        loadInventory(dictionary)

    elif menuoption == 8:
    saveInventory(dictionary)

Again I tried everything to get the dicitonary in that function to output but I'm at my wits end. Any help will be much appreciated!

CodePudding user response:

Your code creates new dictionaries at three different points:

    # this looks like it never gets used for anything
    dicty = {}  
    # this overwrites the "dictionary" passed to loadInventory()
    dictionary= dict((value[0], value[1:]) for value in final)
    # this creates a new dictionary to pass to all the other functions
    dictionary= {}

If you want to only have one dictionary that is shared and updated by all the functions, your code should only create one dictionary (the one you pass into all the functions), and none of the functions themselves should create new dictionaries to return.

I think the line you want to change is that dictionary = dict(...) line in loadInventory -- maybe make that a dictionary.update(...) call?

As a side note, if the intent of all of these functions is to modify the dictionary in-place, they shouldn't return the dictionary at the end. The caller already has a reference to the dictionary it passed in, so returning another reference to the same dictionary just creates potential for confusion.

CodePudding user response:

The problem is that you never assign the returned value of your functions from anything. The only return that matters is when you loadInventory() because it is the function which creates the dictionary. The other functions modify the contents of the existing dictionary, so they don't need to return anything.

  • Related