0

This is a program which asks a user to input a barcode , then my program finds the specified product , asks the user how many of a product they would like to purchase , if they would like to continue , calculates the total price and then is SUPPOSED to print out a reciept,howevery my problem is that the reciept repeats its values and is not rounded too two decimal places.

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']    

I am pretty sure this is where the code is messing up (my reciept code)

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

And this is the whole code as a entity

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

I would appreciate any help with my program or a general pointer to the right direction. Thank you!

  • The problem is that you're trying to store currency as a float -- due to floating point precision you're [going to have a bad time](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). Use `int` for both dollars and cents. – pingul Mar 27 '17 at 21:23
  • Can we double check that the pasted code is the one producing the error? It calls "search_user_input()` without a parameter near the end, and has a line `if continue_shopping !=0 and continue_shopping !=1:` which means the csv file will never be written – Simon Fraser Mar 27 '17 at 21:50
  • I did this to output an error if the user does not enter a valid input: 1 or 0 – whateves123 Mar 28 '17 at 10:16

2 Answers2

0

first try removing all the "global total_price" definitions in your functions.. (it would reset the variable each time) Secondly in your function search_user_input() i would change the line price = round(float(row[2]),2) into price = float(row[2]) (you can later use format to display the final float value in the desired format). Thirdly in your function quantity() you should change the line if continue_shopping !=0 and continue_shopping !=1: into if continue_shopping ==0 or continue_shopping ==1: , good luck

-1

Well, it is not rounded as intended, because you store it as a String first and round it second.

I would recommend to swap the lines values = [str(product), str(price), str(order*price)] and price = str(round(price,2))