如何阻止我的回忆停止重复自己

时间:2017-03-27 21:08:24

标签: python csv

这是一个要求用户输入条形码的程序,然后我的程序找到指定的产品,询问用户他们想要购买多少产品,如果他们想继续,计算总价格和然后被支持打印出一个收据,我的问题是收件人重复它的值并且不是小数点后两位。

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()

这是整个代码作为实体

import csv 
import locale
continue_shopping = 0
total_price = []

locale.setlocale( locale.LC_ALL, '' )
def read_csv_file():
    global total_price
    """ reads csv data and appends each row to list """
    csv_data = []
    with open("task2.csv") as csvfile:
         spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
         for row in spamreader:
             csv_data.append(row)
    return csv_data


def get_user_input():
    global total_price
    """ get input from user """
    while True:
        try:
            GTIN = int(input("input your gtin-8 number: "))
            return GTIN # Breaks the loop and returns the value
        except:
            print ("Oops! That was not a valid number.  Try again")


def search_user_input(product_data):
    global total_price
    repeat=""
    # Pass the csv data as an argument
    """ search csv data for string """
    keep_searching = True

    while keep_searching:
        gtin = get_user_input()
        for row in product_data:
            if row[0] == str(gtin):
                product = row[1]
                price = round(float(row[2]),2)
                return(product, price)
        while True:
            try:
                repeat = input("not in there? search again? If so (y), else press enter to continue")
                break
            except:
                print("please make sure you enter a valid string") 
        if repeat != 'y':
            keep_searching = False 
            return None 


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)]
        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 and continue_shopping !=1:
    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:
        search_user_input()
        quantity()
quantity()

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

2 个答案:

答案 0 :(得分:0)

首先尝试删除所有"全局total_price"函数中的定义..(每次都会重置变量) 其次在函数search_user_input()中,我将行price = round(float(row[2]),2)更改为price = float(row[2])(稍后您可以使用format以所需格式显示最终浮点值)。 第三,在你的函数数量()中你应该将行if continue_shopping !=0 and continue_shopping !=1:改为if continue_shopping ==0 or continue_shopping ==1:,祝你好运

答案 1 :(得分:-1)

好吧,它没有按预期进行舍入,因为您首先将其存储为字符串并将其四舍五入。

我建议交换行values = [str(product), str(price), str(order*price)]price = str(round(price,2))