收录我的代码不断重复自己,并没有考虑其他值[python]

时间:2017-03-28 22:10:57

标签: python csv

首先,我想为英语不好而道歉,因为这不是我的母语。

这是一个要求用户输入条形码的程序,然后我的程序找到指定的产品,询问用户他们想要购买多少产品,如果他们想继续,计算总价格和然后被支持打印出一个收据,我的问题是,收据重复其值并且不是小数点后两位小数。另一个主要问题是我的计算中的脚本只考虑了最后的产品,所以如果我订购2个西红柿和2个芹菜棒并询问价格,它只计算2个芹菜棒

text

我很确定这是代码搞乱的地方(我的收件代码)

press 0 to stop shopping and print your reciept or press 1 to continue shopping0
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '2.80']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.20']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '1.4', '32.199999999999996']
[]
['celery', '0', '0']    

这是整个代码作为实体

def quantity():
    fileOne = open('receipt.csv', 'a')
    writer = csv.writer(fileOne)
    global total_price
    product_data = read_csv_file()
    matches = search_user_input(product_data)
    if matches: # Will not be True if search_user_input returned None
        print("apple")
        product, price = matches[0], matches[1]
        order = int(input("How much of {} do you want?".format(product)))
        values = [str(product), str(price), str(order*price)]
        price = str(round(price,2))
        writer.writerows((values,))
        total_price.append(order * price)
    continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping"))
    if (continue_shopping == 0):
        fileOne.close()
        fileTwo = open("receipt.csv" , "r")
        reader = csv.reader(fileTwo)
        for row in reader:
            if row != None:
                print(row)
    elif continue_shopping==1:
        quantity()

我很感激我对程序的任何帮助或指向正确方向的一般指针。谢谢!

0 个答案:

没有答案