我在这个格式的股票文件:如何附加一个文件,并采取了具体的行蟒蛇3
12345678,Fridge,1,50
23456789,Car,2,50
34567890,TV,20,50
这是代码:
def main():
products = {}
#This is the file directory being made.
f = open('stockfile.txt')
#This is my file being opened.
for line in f:
# Need to strip to eliminate end of line character
line = line[:-1]
#This gets rid of the character which shows and end of line '\n'
row = line.split(',')
#The row is split by the comma
products[row[0]] = [row[1], row[2],row[3]]
#The products are equal to row 1 and row 2 and row 3. The GTIN is going to take the values of the product and price so GTIN 12345678 is going to correspond to Fridge and 1.
print(products)
total = 0
print('Id Description Total')
while True:
GTIN = input('Please input GTIN ')
if(GTIN not in products):
print('Sorry your code was invalid, try again:')
break
row = products[GTIN]
print(GTIN)
description = row[0]
value = row[1]
stock = row[2]
print(stock)
quantity = input('Please also input your quantity required: ')
row[2]= int(stock) - int(quantity)
products[row[2]] = row[2]
product_total= (int(quantity)*int(value))
New_Stock = GTIN + ',' + description + ',' + value + ',' + str(products[row[2]])
f = open('stockfile.txt','r')
lines = f.readlines()
f.close()
f = open("stockfile.txt","a")
for row in lines:
if((row + '\n') != (New_Stock + '\n')):
f.write(New_Stock)
f.close()
print('%20s%20s%20s' % (GTIN, description, product_total))
total = total + product_total
print('Total of the order is £%s' % total)
print(products)
main()
但是,代码不更新股票。它应该做的是摆脱给定产品的先前库存,然后根据用户刚购买的数量进行更新。
我还没有到它,但一旦股票打零,我需要我的代码,然后告诉用户,我们已经缺货,需要一些新的股票。然后需要给用户留言以等待,直到我们进货,然后再显示补货的价格。
如果你有时间请你也可以做这个新的代码,但如果不是,你可以解释如何更新股票以及为什么我的代码不工作,谢谢。
即使在添加之后,我仍然遇到同样的问题。 – HC123
@ HC123你可以发布你的代码吗?以及你的文本文件,当你得到这个错误? – TisteAndii
对不起,我似乎无法出于某种原因......请尝试将您的代码并入我的代码并将其与文件一起发布。我似乎无法出于某种原因发布信息。 @TisteAndii – HC123