Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何阻止我的接收停止重复_Python_Csv - Fatal编程技术网

Python 如何阻止我的接收停止重复

Python 如何阻止我的接收停止重复,python,csv,Python,Csv,这是一个程序,它要求用户输入条形码,然后我的程序找到指定的产品,询问用户想要购买多少产品,如果他们想继续,计算总价格,然后打印接收,然而,我的问题是receipt重复它的值,并且没有四舍五入到小数点后两位 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

这是一个程序,它要求用户输入条形码,然后我的程序找到指定的产品,询问用户想要购买多少产品,如果他们想继续,计算总价格,然后打印接收,然而,我的问题是receipt重复它的值,并且没有四舍五入到小数点后两位

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']    
我很确定这就是代码混乱的地方(我的接收代码)

这是作为一个实体的整个代码

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

我将非常感谢您对我的程序的任何帮助或指向正确方向的一般指针。谢谢大家!

嗯,它并没有按预期的方式四舍五入,因为您先将其存储为字符串,然后再将其四舍五入


我建议交换行
values=[str(product)、str(price)、str(order*price)]
price=str(round(price,2))
首先尝试删除函数中的所有“全局总价”定义。。(每次都会重置变量) 其次,在函数search\u user\u input()中,我会将行
price=round(float(row[2]),2)
更改为
price=float(row[2])
(以后可以使用格式以所需格式显示最终的浮点值)。
第三,在函数quantity()中,如果继续购物,则应更改行
=0并继续购物=1:
进入
如果continue\u shopping==0或continue\u shopping==1:
,祝您好运

问题是您正试图将货币存储为浮点数--由于您使用的是浮点数精度。对美元和美分都使用
int
。我们可以再次检查粘贴的代码是否是产生错误的代码吗?它调用“search_user_input()”,结尾附近没有参数,如果continue_shopping!=0和continue_shopping!=1:,则会有一行
,这意味着永远不会写入csv文件。如果用户未输入有效输入,则会输出错误:1或0
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()