为什么这个程序不工作它告诉我项目找不到即使它在CSV文件里面它根本不想找到代码它找不到任何东西总是找不到它也不在乎如果我按Y继续它不在乎如果我按L它仍然会继续
print(" Description Product ID Price")
import csv
f = open('CSVExelStockControlTask2.csv')
stock = csv.reader(f)
stocklist = []
for each in stock:
print(each)
print("This is the list of what we have in-stock")
instock = []
outofstock = []
Repeat = True
found = False
while Repeat == True:
item = int(input('Enter a GTIN Code of the item you want to add: '))
for thing in stock:
name = thing[0]
GTIN = thing[1]
if GTIN == item:
found = True
temp = []
print ('\nItem Found',name , '-' ,GTIN ,'\n')
temp.append(item)
temp.append(GTIN)
instock.append(temp)
if found == False:
print ('Item Was Not Found')
outofstock.append(item)
Cont = input('\nAdd another item?')
if Cont == 'N' or Cont == 'n':
Repeat=False
print ('\nItem Selected')
for each in instock:
print (each)
print ('\nItems not found')
for each in outofstock:
print(each)
应该是这样的:
Description Product ID Price
['100mm Bolts', '54378438', '0.5']
['Plain Brackets', '17578455', '2']
['Mounting Screws', '84257420', '3']
['Alarm Clock', '46325754', '6']
['Hedgehog Chushin', '22541529', '400']
['Bambo Couch', '17613422', '350']
['Wooden King Bed Frame', '45632464', '150']
['Mirror', '23454323', '75']
['Door', '36546756', '56']
['Window', '36546541', '30']
This is the list of what we have in-stock
Enter a GTIN Code of the item you want to add: 36546541
Item Was Found
Add another item?N
Item Selected
36546541
Items not found
但结果却是:
Description Product ID Price
['100mm Bolts', '54378438', '0.5']
['Plain Brackets', '17578455', '2']
['Mounting Screws', '84257420', '3']
['Alarm Clock', '46325754', '6']
['Hedgehog Chushin', '22541529', '400']
['Bambo Couch', '17613422', '350']
['Wooden King Bed Frame', '45632464', '150']
['Mirror', '23454323', '75']
['Door', '36546756', '56']
['Window', '36546541', '30']
This is the list of what we have in-stock
Enter a GTIN Code of the item you want to add: 36546541
Item Was Not Found
Add another item?N
Item Selected
Items not found
36546541
有人能帮我吗?你知道吗
检查失败的原因是CSV模块将所有内容都读取为字符串。你说
item = int(input(...
,然后和if GTIN == item:
比较。这将始终失败,因为int永远不会与字符串进行比较。要解决此问题,请与if int(GTIN) == item
进行比较,或者不转换为int(改用item = input(...
)。你知道吗另一个问题是迭代
stock
两次。在第二个循环中,它根本不会运行。你需要把它读成一个列表。使用stock = list(csv.reader(f))
相关问题 更多 >
编程相关推荐