Python 仅显示.csv文件的最后一行



我正在尝试创建一个程序(作为受控评估的一部分),该程序允许用户从excel(.csv)文件中输入3个GTIN产品代码。然后,这将显示有关每个产品的信息,然后询问您要购买多少产品,然后再给出该产品的小计。输入所有三个代码后,您可以选择显示所有产品的收据。总数工作正常,但是当我尝试让它打印产品的实际名称时,它只是在Excel文件的最后一行打印产品名称,在三行上打印三次。

(我知道这段代码可能非常笨拙,但我基本上和你会发现的一样新手。

import sys
while 1==1:
    def gtin1():
        global total
        global product1
        product_ordered=input("Input GTIN ")
        f=open("Task 2 Product Spreadsheet.csv","r")
        for line in f:
            items =line.split(",")
            ordernum=items[0]
            product1=items[1]
            price=float(items[2])
            in_stock=int(items[3])
        if ordernum==product_ordered:
            print("Items Ordered: ")
            print("Item name: ", product1)
            print("Number in stock: " , in_stock)
            print("Price: £",price)
            number = int(input("Input the number of items to be ordered: "))
            total = int(price) * int(number)
            if number <= in_stock:
                print("The total price is £" + str(total))
                print ("")
            else:
                print ("There is insufficient stock.")
                print ("")
def gtin2():
    global total2
    global item_name2
    product_ordered=input("Input GTIN ")
    f=open("Task 2 Product Spreadsheet.csv","r")
    for line in f:
        items =line.split(",")
        ordernum=items[0]
        item_name2=items[1]
        price=float(items[2])
        in_stock=int(items[3])
        if ordernum==product_ordered:
            print("Items Ordered: ")
            print("Item name: ", item_name2)
            print("Number in stock: " , in_stock)
            print("Price: £",price)
            number = int(input("Input the number of items to be ordered: "))
            total2 = int(price) * int(number)
            if number <= in_stock:
                print("The total price is £" + str(total2))
                print ("")
            else:
                print ("There is insufficient stock.")
                print ("")
def gtin3 ():
    global total3
    global item_name3
    product_ordered=input("Input GTIN ")
    f=open("Task 2 Product Spreadsheet.csv","r")
    for line in f:
        items =line.split(",")
        ordernum=items[0]
        item_name3=items[1]
        price=float(items[2])
        in_stock=int(items[3])
        if ordernum==product_ordered:
            print("Items Ordered: ")
            print("Item name: ", item_name3)
            print("Number in stock: " , in_stock)
            print("Price: £",price)
            number = int(input("Input the number of items to be ordered: "))
            total3 = int(price) * int(number)
            if number <= in_stock:
                print("The total price is £" + str(total3))
                print ("")
            else:
                print ("There is insufficient stock.")
                print ("")
                break
def receipt():
    receipt = int(total) + int(total2) + int(total3)
    print ("")
    print ("")
    print (product1)
    print (item_name2)
    print (item_name3)
    print ("The total price of your order is: £" + str(receipt))
    print ("")
    print ("")
menu = input("You may enter 3 GTIN codes. Press '1' for your first code, '2' for your 
second, and '3' for your third, 'r' to see your receipt, or 'c' to exit the program.")
if menu == "1":
    gtin1()
elif menu == "2":
    gtin2()
elif menu == "3":
    gtin3()
elif menu == "r":
    receipt()
elif menu == "c":
    print ("Thank you for using the program.")
    sys.exit()
else:
    print("Enter a valid command.")

但是当我尝试让它打印产品的实际名称时,它 只需在Excel的最后一行打印产品名称 文件,在三个单独的行上三次。

这是正常现象,也是代码的预期行为。

实际上,对于 CSV 文件的每一,您都设置了 product1、item_name2 或 item_name3。

如果订单号是你想看的那个,你然后问用户他想买多少/订购。

但是,在那之后,foreach 循环将继续并读取文件直到最后(即使没有更多的打印)。最后,为 3 个项目名称设置最后一个 CSV 行值。

只需 if ordernum==product_ordered:

为了停止读取文件,因为您不需要它!然后,它将停止更新项目名称并保留所需的项目名称。

相关内容

  • 没有找到相关文章

最新更新