0

List function questions

So let’s pretend that I have a program that reads lines from from a txt file and converts them into a list def vendInventory(): Example of line one in txt file : A1 soda $1.50 3 Once it is read and stored as a list within my function it looks like [A1, soda, $1.50, 3] The first 3 elements don’t matter because they will remain the same but the fourth element I need to be an int so that I can subtract the number of items purchased from its inventory when a purchase is made(when I will call the function in my main loop) and then then write it to a temp file and strip it to where it has no commas or brackets for printing In your opinion what would the most efficient way be Let’s pretend you have created the function and you’ve opened the text file in read mode and temp file in write mode you need to read text file then convert it to a list with the first 3 elements being strings and the 4th as an int then you need to subtract the numItems purchased from that int and store it in the temp file then delete the text file and rename the temp file with the name text file using os this way can call your read function to display the vending machine options and use the vendInventory function to subtract from the inventory when items are purchased

19th Mar 2021, 6:35 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
2 Answers
+ 1
If I needed to keep some kind of log I would only append to a file and only write strings there. And if an item needs to be subtracted I would append a record with a negative value. When new items come in I would append a record with a positive number. On a daily basis I would create a new file. If I needed a database with the current stock I would probably use a database such as MySql. Both can be implemented simultaneously. In the database I would keep a table with Id, product name and price and another table with ID, current number of items in inventory. If the name or price changes I would use a new ID and deal with the remaining items as the accountant says :)
19th Mar 2021, 6:53 AM
Eddy M
0
do i need to convert the int back into a string so that all elements are strings before writing it back to the temp file?
19th Mar 2021, 6:37 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar