如何阻止我的收信人停止重复自己 [英] How to stop my reciept stop repeating itself
问题描述
这是一个程序,要求用户输入条形码,然后我的程序找到指定的产品,询问用户他们要购买多少产品,如果他们想继续购买,计算总价格并然后应该打印出一个收据,但是我的问题是该收据重复其值并且不能四舍五入到小数点后两位.
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!
推荐答案
首先尝试删除函数中的所有全局total_price"定义.(每次都会重置变量)
其次,在函数search_user_input()中,我将行price = round(float(row[2]),2)
更改为price = float(row[2])
(您以后可以使用format以所需的格式显示最终的float值).
第三,在函数quantity()中,应将行if continue_shopping !=0 and continue_shopping !=1:
更改为if continue_shopping ==0 or continue_shopping ==1:
,祝您好运
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
这篇关于如何阻止我的收信人停止重复自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!