2017-03-27 49 views
0

這是一個要求用戶輸入條形碼的程序,然後我的程序找到指定的產品,詢問用戶他們想要購買多少產品,如果他們想要繼續,計算總價格,然後用SUPPOSED打印一份收據,但我的問題是收款人重複其價值,並且沒有舍入兩位小數。如何阻止我的收件人停止重複

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

我將不勝感激任何對我的程序的幫助或對正確方向的一般指針。謝謝!

+0

問題是你試圖將貨幣存儲爲浮點數 - 由於浮點精度,一個不好的時間](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)。美元和美分使用'int'。 – pingul

+0

我們可以仔細檢查粘貼的代碼是否是產生錯誤的代碼?它調用「search_user_input()'在沒有參數的情況下結束,並且有一行'if continue_shopping!= 0 and continue_shopping!= 1:'這意味着csv文件將永遠不會被寫入 –

+0

我這樣做是爲了輸出錯誤用戶不輸入有效的輸入:1或0 – whateves123

回答

-1

那麼,它不是按照預期圓整的,因爲你先把它作爲一個字符串存儲起來,然後把它存儲起來。

我會建議換線​​3210和price = str(round(price,2))

+0

仍然不工作:( – whateves123

0

首先嚐試刪除所有「全球TOTAL_PRICE」定義你的功能。其次(這將重置每次變量) 在你的函數search_user_input()我會將行price = round(float(row[2]),2)更改爲price = float(row[2])(您可以稍後使用格式以所需的格式顯示最終的浮點值)。 第三在你的函數數量()中,你應該將行if continue_shopping !=0 and continue_shopping !=1:更改爲if continue_shopping ==0 or continue_shopping ==1:,祝你好運