我想循环代码让用户添加另一个项目。
我的任务是“开发一个程序,它将在订单后更新库存水平。在库存文件中包括当前库存水平,重新订购水平和目标库存水平。该程序应在指示时,计算哪些产品缺货或低于重新订购水平并创建一个文件。该文件将包含重新库存的订单,这些订单将使这些产品的当前库存水平达到目标库存水平“。我已经做了所有这些,但是我很难添加while循环,以防用户希望在他们的订单中添加另一个项目。
This is the stock file (this is named product information in the code)
This is the updated stock file when I run the code (this is named Blank file in the code)
This is the text file which contains orders for restocking (this is named Reorders in the code)
这是我的代码:
import csv
option='yes'
while option=='yes':
data=open('Product information.csv', 'rt')
purchase=csv.reader(data)
blank=open('Blank file.csv', 'wt', newline='')
blank_write=csv.writer(blank)
reorder=open('Reorders.txt', 'wt')
user_orders=[]
order=input('Please enter the GTIN-8 code of the product you would like to purchase: ')
quantity=int(input('Please enter the amount of this product you want to buy: '))
for row in purchase:
GTIN=row[0]
item=row[1]
price=row[2]
stock=row[3]
reorder_level=row[4]
target_stock=row[5]
if order==GTIN:
stock=int(stock)-quantity
blank_write.writerows([[GTIN,item,price,stock,reorder_level,target_stock]])
data.close()
blank.close()
blank2=open('Blank file.csv', 'rt')
blank_write2=csv.reader(blank2)
choice=input('Please enter yes to check the current stock levels: ')
if choice=='yes':
for row in blank_write2:
GTIN=row[0]
item=row[1]
price=row[2]
stock=int(row[3])
reorder_level=int(row[4])
target_stock=int(row[5])
if stock<=reorder_level:
amount_to_restock=target_stock-stock
reorder_file=('{} {} {} {} {} {} {}'.format(row[0]+' ', row[1]+' ', '{:10.2f}'.format(float(row[2]))+' ', str(stock)+' ', str(reorder_level)+' ', str(target_stock)+' ', str(amount_to_restock)))
reorder.write(reorder_file)
option=input('Would you like to add another item: ')
if option=='no':
blank2.close()
reorder.close()
如果我输入“是否要添加其他项目”,则最新产品会覆盖我订购的第一个产品。这意味着只有最新产品才会写入CSV文件。我希望程序将这两个产品写入CSV文件。
由于