Could you help with this code i'm working on


#1



I want it to check a file to see if anything is below a certain stock level and then create a new file with the order for it.


try:
    stock_file = open ("stock.txt", "r")
    stock_items = stock_file.split(" : ")
except:
    print ("Couldn't open file!")
    stock_file.close()

doc_minimal = stock_items[3]
doc_current = stock_items[4]
doc_target = stock_items[5]

for aline in stock_file:
    re_stock = (int(doc_target)) - (int(doc_current))
    print (re_stock)
    reorder_file = open("re_ordering.txt", "a")
    reorder_file.write ("\n" + re_stock)
    reorder_file.write ("\n---------------------------------\n")
    reoder_file.close()

stock_file.close()


#2

this line:

stock_items = stock_file.split(" : ")

you only want to do this if the try is successful. this line:

stock_file.close()

you don't need to close the file, if you can't open it in the first place, so then you get this:

try:
    stock_file = open ("stock.txt", "r")
except:
    print ("Couldn't open file!")

stock_items = stock_file.split(" : ")

however, before you split i would loop over the lines. You can't split a file like this. This should help you further a bit.

If you need more help, post an updated version of your code (and the content of stock.txt would also be nice)

But first try further yourself now i gave you a bit of direction


#3

Here the stock file:

12345670 : Chicken Curry : 7.00 : 100 : 50 : 150 :
45873423 : Wooden Plank : 14.99 : 80 :50 : 100 :
35826903 : Plant Seeds : 5.50 : 150 : 100 : 200 :
37620912 : Shampoo : 2.00 : 50: 30 : 100 :
57693309 : Air Fresher : 4.65 : 90 : 50 : 100 :
47001275 : Conditioner : 2.00 : 90 : 50 : 100 :
54321762 : Tooth Paste : 1.85 : 100 : 50 : 150 :
79466530 : Toilet Paper : 2.99 : 150 : 100 : 200 :
15568700 : Heinz Beans : 3.99 : 50 : 30 : 100 :
11160977 : Tomato Ketchup : 2.65 : 50 : 30 : 100 :
25478969 : Instant Coffee : 4.00 : 100 : 50 : 150 :
34697825 : Mayonaise : 2.65 : 80 : 50 : 100 :
44653828 : Chicken : 5.99 : 50 : 30 : 10 :
65483183 : Pork : 5.85 : 100 : 50 : 150 :
79465380 : Steak : 6.50 : 80 : 50 : 100 :
12365708 : Diet Coke : 3.25 : 150 : 100 : 200 :


#4

it seems you are already looping over the lines:

for aline in stock_file:

this is where you should apply the split. what should the program do?


#5

Keep in mind that it's much easier to provide relevant help if you explain what the trouble is, what you're unable to do, what you want to know. Otherwise people may become very reluctant to tell much about anything since it's not known what you're actually looking for and anything said is fairly likely to be something you don't care about, a waste of effort.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.