我会尝试尽可能地简化这个,我的代码是为用户设计的,使用条形码从数据库输入多个产品,导致我的程序以他们订购的产品名称和总数输出产品价格。
但是,首次运行时我预期的代码输出是
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")
但是,由于某种原因,此代码首先在代码末尾作为起始行运行。
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:
print("make sure you enter a valid number")
我很感激我的代码有任何有用的输入或批评。
如果您对此感兴趣,那么代码就是一个独立的实体。感谢您的帮助
import csv
import locale
from decimal import *
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)))
TWOPLACES = Decimal(10) ** -2
subt = order * pricea
subtotal = Decimal(subt).quantize(TWOPLACES)
values = [str(product), str(price), str(subtotal)]
print('values are ',values)
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:
print("make sure you enter a valid number")
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()
quantity()
答案 0 :(得分:0)
当你创建一个函数(def的作用是什么)时,它不会做任何事情,直到你实例化该函数。所以,例如,如果你想在询问这个人是否完成购物之前运行read_csv_file()函数,你必须这样做。 (或类似的东西)
#all your functions are above this snippet.
read_csv_file()
continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping"))
我希望这有帮助!