im new to this site and I know alot of people aren't very happy when somebody asks a question previously asked. However, I wish to ask despite it being previously asked beacause all the answers I found did not make much sense to me (im new to python!), so I was wondering if somebody could dumb it down for me or directly correct my code.
我是这个网站的新手,我知道很多人在别人问之前问过的问题时都不太高兴。然而,我想问的是,尽管它之前被问过,因为我发现的所有答案对我来说都不太有意义(我是python的新手!),所以我想知道是否有人可以为我做一些傻事,或者直接更正我的代码。
Im writing a code where the user inputs a GTIN-8 code and it searches a csv excel file for that code, it then reads the appropriate information about the product (price,ect...) and prints it out. However I cant search the second line of the file for some reason. Here is my code:
我在编写一个代码,用户输入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()`
1 个解决方案
#1
1
You are reading second line but because of the break
, you never get a chance to use it since your code always breaks out of while loop if it enters there. Just remove it and your code should work fine.
您正在读取第二行代码,但是由于中断,您永远不会有机会使用它,因为如果代码进入了while循环,您的代码总是会中断。只要删除它,您的代码就可以正常工作。
Also, assuming your syntax is correct on this line.
此外,假设您的语法在这一行上是正确的。
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
#1
1
You are reading second line but because of the break
, you never get a chance to use it since your code always breaks out of while loop if it enters there. Just remove it and your code should work fine.
您正在读取第二行代码,但是由于中断,您永远不会有机会使用它,因为如果代码进入了while循环,您的代码总是会中断。只要删除它,您的代码就可以正常工作。
Also, assuming your syntax is correct on this line.
此外,假设您的语法在这一行上是正确的。
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