简体   繁体   中英

Python only displays the last line of a .csv file

I'm trying to create a program (as part of a controlled assessment), which allows the user to enter 3 GTIN product codes from an excel (.csv) file. This then displays information about each product, and then asks how many of the product you want to buy, before giving a subtotal for that product. Once all three codes are inputted, you can choose to display a receipt of all products. The total works fine, but when I try to have it print the actual names of the products, it just prints the name of the product on the last line of the Excel file, three times on three separate lines.

(I know this code is probably ridiculously clumsy, but I am basically as much of a novice as you'll find.)

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

but when I try to have it print the actual names of the products, it just prints the name of the product on the last line of the Excel file, three times on three separate lines.

This is normal and the expected behaviour of your code.

In fact, for each line of your CSV file, you set product1, item_name2 or item_name3.

If the ordernum if the one you want to look at, you then ask the user for how much he want to buy/order.

However and after that, the foreach loop will continue and read the file until the end (even if there is no more prints). Ending by setting the last CSV line value for your 3 item names.

Simply return your function (or break from the for loop) at the end of if ordernum==product_ordered:

In order to stop reading file, as you don't need it ! It will then stop updating the item names and keep the one you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM