I'm doing an inventory system which require for me to print distribution list of particular item. If the particular item has distribute to the same hospital for more than 1 time, it need to sum the quantity together.
The code that i'm writing now only allow for me to print the distribution list, it won't sum the quantity together if the item code and hospital code is same. (Check with code but not name to sum it up) How can i make it sum up together if hospital code and item code is same?
I'm only allow to use list, dictionary and pandas are not allowed.
def search_functions():
try:
fhand = open("distribution.txt","r")
except:
print("You haven't distribute any item yet!\n")
searchInput = input("\nEnter item code to search: ")
print()
for line in fhand:
lines = line.strip()
if searchInput.lower() in lines.lower():
print(lines)
continue
print()
fhand.close()
search_functions()
Inside the text file
first item is hospital code, second is name, 3rd is item code, 4th is item name and the last one is quantity.
distribution.txt text file
HSPT001,Care Medical Hospital,HC,Head Cover,9
HSPT001,Care Medical Hospital,HC,Head Cover,6
HSPT002,Community Health Service,HC,Head Cover,7
The output need to be like below, when the searchinput is HC
HSPT001, Care Medical Hospital, HC, Head Cover, 15
HSPT002, Community Health Service, HC, Head Cover, 7
instead of
HSPT001,Care Medical Hospital,HC,Head Cover,9
HSPT001,Care Medical Hospital,HC,Head Cover,6
HSPT002, Community Health Service, HC, Head Cover, 7