我的商店程序代码有问题。如果用户在我的文件(products.csv)中输入了有效的GTIN-8号码,程序将运行正常。但是,如果用户输入了不在文件中的有效GTIN-8代码,则我的代码无效。
我想要打印'找不到代码',然后重复该程序。 目前,它只是“未找到”然后“你想购买多少”
我正在使用found = None,我很确定这是错误的但不确定要改变什么。
(参见checkfile功能)
def getproductcode():
valid = False
while not valid:
code = input("Please enter the product code")
valid = validatecode(code)
return code
def validatecode(code):
valid = False
while True:
if len(code) == 8:
for i in code:
try:
int(i)
valid = True
except ValueError:
print("You have entered an invalid code type. Product codes are 8 numbers long. Please try again.")
return False
valid = checkvalidity(code)
return valid
else:
print("You have entered an invalid code type. Product codes are 8 numbers long. Please try again.")
return False
def checkvalidity(code):
number = code
total = int(number[0]) * 3 + int(number[1]) + int(number[2]) * 3 + int(number[3]) + int(number[4]) *3 + int(number[5]) + int(number[6]) * 3 + int(number[7])
if total%10 == 0:
check = 0
print("Valid.")
return True
else:
print("Invalid. Please try again.")
return False
import csv
def checkfile(code):
found = None # This seems redundant?
with open('products.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
for line in reader:
print(line) # To see what you're getting
if code == line[0]:
print("We found your product ", code)
return line
print(code, "is not found. Please try again")
def quantityFunction():
valid = False
while True:
quantity = input("Please enter the amount of this item you would like to purchase: ")
for i in quantity:
try:
int(i)
#repeat()
return int(quantity)
except ValueError:
print("We didn't recognise that number. Please try again.")
return int(quantity)
def repeat():
print("Would you like to add another product?")
print("1. Yes")
repeatchoice = input("2. No")
if repeatchoice == '1':
return True
elif repeatchoice == '2':
return False
else:
print("We didn't recognise your choice. Please try again")
repeat()
import datetime
def append(product, quantity):
f = open("reciepts","a")
#time = str(datetime.datetime.now())
#f.write(time)
#for i in range(len(product)):
#1 product
# f.write(product[i])
f.write(product)
quantity = str(quantity)
f.write(quantity)
with open('products.csv', newline='') as f:
reader = csv.reader(f, delimiter='\t')
for line in f:
print(line[1])
# f.write(product)
f.close
def reciept():
print("Here is your reciept:")
f = open("reciepts","r")
print(f.read())
#print("Here you are",f.readline())
print("Thank you for shopping with Shoesmith's Home & Office Supplies Superstore. Please come again soon")
print("Goodbye")
#rather than just print the file, read in each data to have decent formatting. i.e f.read data(1)
#main
f = open("reciepts","w")
f.write("")
f.close
repeatchoice = repeat()
repeatchoice == True
while repeatchoice == True:
code = getproductcode()
#print(code)
product = checkfile(code)
#print(product)
quantity = quantityFunction()
print(quantity)
print(repeatchoice)
addtoreciept = append(product, quantity)
print(repeatchoice)
repeatchoice = repeat()
print(repeatchoice)
#print(repeatchoice)
reciept()
#print(product, quantity)
'''with open('products.csv', newline='') as f:
reader = csv.reader(f, delimiter='\t')
for line in f:
data = line.split(",")
if code == data[0]:
print("We found your product ",data)
return data'''
我的csv看起来像这样;
任何想法? 感谢
答案 0 :(得分:0)
首先,请尝试减少您在一个块中发布的代码量。从您的问题可以清楚地看出,您认为错误出现在checkfile
函数中 - 请考虑粘贴该函数。
checkfile
功能除了打印未找到代码外,如果有无效代码,则不会执行任何操作。特别是,它不会返回任何内容,这意味着它将返回None。在print语句之后放置你想要的任何行为。
声明found = None
似乎毫无意义,除非found
用于未粘贴的部分代码中。
答案 1 :(得分:0)
它可能不是最有效的方式,但似乎有效......
我现在在我的几个函数中使用if product is not None:
来阻止代码跳过quantityFunction。