Nathan Shoesmith Nathan Shoesmith - 6 months ago 33
Python Question

Replacement for found = None?

I have a problem with my shop program code. If the user enters a valid GTIN-8 number in my file (products.csv), the program goes through and works fine. However, if the user enters a valid GTIN-8 code that is not in the file, my code is not working.

I want it to print 'code is not found' and then repeat the program.
At the moment, it's just going to 'not found' then 'how many would you like to purchase'

I'm using found = None which I'm pretty sure is wrong but not sure what to change.

(See checkfile function)

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


My csv looks like this;


  • 34512340 Plain Brackets 0.5

  • 12395675 Yellow Pencil Sharpener 2

  • 56756777 100mm Bolts 0.2

  • 90673412 L-Shaped Brackets 1.2



Any Ideas?
Thanks

Answer

It's probably not the most efficient way, but it seems to be working...

I am now using if product is not None: in several of my functions which stops the code from skipping into the quantityFunction.

Comments