如果用户希望购买另一项",当他们这样做时,新产品将替换CSV文件中的旧产品。
This is my CSV file which contains all the product information:
我的任务是开发一个程序,在订单后更新库存水平。在库存文件中包括当前库存水平,重新订购水平和目标库存水平。该程序在被指示时应计算哪些产品缺货或低于重新订购水平。
这是我的代码:
import csv
option='yes'
while option=='yes':
data=open('Product information.csv', 'rt')
purchase=csv.reader(data)
blank=open('Blank file.csv', 'wt')
blank_read=csv.writer(blank)
order=input('Please enter the GTIN-8 code of the product you would like to purchase: ')
for row in purchase:
for field in row:#
if field==order:
quantity=int(input('How much of that item: '))
if quantity<=int(row[3]):
print('Your order has successfully been added')
difference=int(row[3])-int(row[4])
if quantity>=difference:
stock=int(row[3])-quantity
GTIN=row[0]
item=row[1]
price=row[2]
stock=row[3]
reorder=row[4]
target_stock=row[5]
blank_read.writerows([[GTIN,item,price,stock,reorder,target_stock]])
if quantity>int(row[3]):
print('You have exceeded the stock level')
option=input('Do you want to purchase another item: ')
blank.close()
另外,如何将最新的库存水平更新到我的空白CSV文件中,因为当用户购买商品时,当前库存将明显改变。对不起,如果我要求很多!!!
感谢您的帮助!!!!
答案 0 :(得分:0)
您将在此处查看所有剩余的购买内容:
for order in purchase:
if quantity>=difference:
删除for order in purchase:
,它应该更好用:
import csv
option='yes'
while option=='yes':
data=open('Product information.csv', 'rt')
purchase=csv.reader(data)
blank=open('Blank file.csv', 'wt')
blank_read=csv.writer(blank)
order=input('Please enter the GTIN-8 code of the product you would like to purchase: ')
for row in purchase:
for field in row:
if field==order:
quantity=int(input('How much of that item: '))
if quantity<=int(row[3]):
print('Your order has successfully been added')
difference=int(row[3])-int(row[4])
if quantity>=difference:
stock=int(row[3])-quantity
GTIN=row[0]
item=row[1]
price=row[2]
stock=row[3]
reorder=row[4]
target_stock=row[5]
blank_read.writerows([[GTIN, item, price, stock,
reorder, target_stock]])
if quantity>int(row[3]):
print('You have exceeded the stock level')
option=input('Do you want to purchase another item: ')
blank.close()
答案 1 :(得分:0)
好的,所以我看到MikeMüller已经回答了你的问题。然而,作为迈克,我不太了解,我还花了一些时间来解决你的问题,然后才弄明白。然而,我想对迈克的回答做一些补充。
首先,我必须指出你的问题对我来说仍然不太清楚。一方面,你质疑为什么你得到重复的输出。迈克解释并告诉你为什么会这样。但与此同时,您指出“程序应该在被指示时计算哪些产品缺货或低于重新订购级别”,这与您的代码目前正在做的不完全相同。现在它只从当前库存stock=int(row[3])-quantity
中提取所需的项目数量,但在写入新的CSV文档之前,只有几行后原始值被重置stock=row[3]
。更重要的是,如果将if quantity<=int(row[3]):
评估为true,则会打印出已成功添加订单的消息,但稍后当if quantity>=difference:
返回false时,没有任何反应。
此外,你在同一个 if语句中启动一个if语句if quantity<=int(row[3]):
和,你调用另一个if语句if quantity>int(row[3]):
,根据你的第一个if语句import csv
blank_file=open('Blank file.csv', 'w')
data_file=open('Product information.csv', 'rt')
do_purchase=csv.reader(data_file)
def makeCSV(user_option):
if user_option=="yes":
new_order=input('Please enter the GTIN-8 code of the product you would like to purchase: ')
global do_purchase
for row in do_purchase:
print ("row =", row[0])
if row[0]== new_order:
total_quantity=int(row[3])
reorder_quantity=int(row[4])
target_quantity=row[5]
required_quantity=int(input('How much of that item: '))
if required_quantity<=total_quantity:
print("yes")
max_order=total_quantity-reorder_quantity
if required_quantity<=max_order:
stock=total_quantity-required_quantity
print("new stock =", stock)
GTIN=row[0]
item=row[1]
price=row[2]
reorder=row[4]
target_stock=row[5]
global blank_file
blank_file_read=csv.writer(blank_file)
blank_file_read.writerows([[GTIN,item,price,stock,reorder,target_stock]])
break
else:
print('Sorry, we can only deliver', max_order ,'items')
break
else:
print("sorry, not enough in stock")
break
user_option=input('Do you want to purchase another item: ')
if user_option == "yes":
makeCSV("yes")
else:
blank_file.close()
exit
else:
blank_file.close()
exit
makeCSV("yes")
所以,我一直在搞乱你的代码,这是我的替代方案(这仍然远非完美,但应该为你提供一些关于如何实现你的一些目标的进一步见解):
initBooks(response) {
console.log(this.state.books, "new books not set yet")
let newBooks = []
for (let entry of response) {
newBooks.push({
id: entry._id,
name: entry.name,
author: entry.author,
description: entry.description,
price: Number(entry.name),
publisher: entry.publisher
})
}
this.setState({books: [...this.state.books, newBooks]}, () => {
console.log(this.state.books, "new books set in the state")
})
}