我是这个网站的新手,我知道当有人问起之前提出的问题时,很多人并不高兴。但是,我想问一下,尽管先前被问过因为我找到的所有答案对我来说都没有多大意义(我刚接触到python!),所以我想知道是否有人可以为我愚蠢或直接纠正我的代码。
我在用户输入GTIN-8代码时编写代码,然后在csv excel文件中搜索该代码,然后读取有关产品的相应信息(价格,等等)并将其打印出来。但是由于某种原因我无法搜索文件的第二行。这是我的代码:
#csv is imported to read/write to the file
import csv
#Each Product is printed alongside it's GTIN-8 code and Price
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("~ Welcome to Toms bits and bobs ~")
print("Pencil, 12346554, £0.40")
print("50 Staples, 12346882, £1.00")
print("50 Paper Clips, 12346875, £1.20")
print("Large Eraser, 12346844, £1.50")
print("100 A4 Sheets, 12346868, £2.00")
print("100 A3 Sheets, 12346837, £2.50")
print("25 Byro Pens, 12346820, £2.20")
print("Handwriting Pen, 12346899, £5.50")
print("50 Split Pins, 12346813, £0.60")
print("Office Chair, 12346912, £25.00")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#The file is opened and the user inputs the code for the product they
#wish to find.
file = open("Product_list.csv", "r")
purchase = input(print("Please enter the GTIN-8 code of the product you wish to purchase e.g 12346554"))
line = file.readline()
data = line.split(",")
if data[0] == purchase:
while(line):
print ("Product: ", data[1])
print ("GTIN-8 code: ", data[0])
print ("Stock: ", data[2])
print ("Description: ", data[3])
print ("Price: ", data[4])
line = file.readline()
break
else:
print("Product not found")
file.close()`
答案 0 :(得分:1)
您正在阅读第二行,但由于break
,您永远不会有机会使用它,因为如果代码进入那里,您的代码总会突然出现while循环。只需删除它,您的代码就可以正常工作。
另外,假设您的语法在此行上是正确的。
purchase = input(print("Please enter the GTIN-8 code of the product you wish to purchase e.g 12346554"))
^^^^^This will cause a syntax error. You should remove this print as well