2017-01-17 50 views
1

我試圖創建一個程序(作爲受控評估的一部分),它允許用戶從Excel中輸入3個GTIN產品代碼(.csv)文件。然後顯示每個產品的信息,然後在給出該產品的小計之前詢問您想購買多少產品。一旦輸入了所有三個代碼,您可以選擇顯示所有產品的收據。總的來說工作正常,但是當我嘗試打印產品的實際名稱時,它只是在Excel文件的最後一行打印產品名稱,三次分開三行。Python只顯示.csv文件的最後一行

(我知道這個代碼可能可笑笨拙,但我基本上一樣多,你會發現一個新手。)

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.") 

回答

2

但是當我試圖把它打印的實際名稱的產品,它只在Excel 文件的最後一行上打印產品名稱,在三個單獨的行上打印三次。

這是正常的和您的代碼的預期行爲。

實際上,您的CSV文件的每行的設置了product1,item_name2或item_name3。

如果ordernum如果您想查看的那個,那麼您會詢問用戶他想要購買/訂購多少。

但是之後,foreach循環將繼續並讀取文件直到結束(即使沒有更多打印件)。 通過設置3個項目名稱的最後一個CSV行值結束。

只需(從或休息 for循環)在 if ordernum==product_ordered:

末返回你的函數爲了停止讀取文件,因爲你並不需要它! 然後它將停止更新項目名稱並保留您想要的項目名稱。

+1

感謝** break **功能,它現在打印出用戶輸入的產品名稱......您可能已經保存了我的課程作業! –

+0

很高興幫助,下一步你應該刪除這3個功能,只使用一個!在gtin()函數的末尾使用返回值,以返回名稱變量。那麼你應該可以使用name1 = gtin()或name2 = gtin()...直接在你的菜單中! :) – Retsim