Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GTIN-8 Code
#1
I'm struggling with some coding. The question is "Create a suitable text file to use with a high-level programming language containing a list of product details, including a GTIN-8 product code, a product description and price. The program should allow a user to enter GTIN-8 codes for a list of products they wish to purchase and the quantity required of each product. The program should search the stock file to produce a list of products with their descriptions, prices, cost for each of the quantity selected and the total cost for all of the products. The program should also identify products not found."

The bit i'm struggling is that i'm not sure how to fetch information from the csv and when the user inputs a GTIN-8 code, for the corresponding product to be printed out.

order=[]
def getData():
    data = []
    playerData = open('products.csv', mode='r')
    for line in playerData:
        line = line.rstrip()
        data.append(line.split(','))
    return data
    
def displayProducts(data):
    for i in data:  
        print("GTIN: {:12.12} Product: {:12.12} Price {:1.2f}".format(i[0],i[1],float(i[2]))) 

def main():
    product =[]
    product = getData()
    print(product)
    displayProducts(product)

main()

correct=False
while not correct:
    validate = input("Input your 8 digit GTIN-8 number")
    if len(validate) == 8 and validate.isdigit():
        correct=True
        Number1=int(validate[0])
        Number2=int(validate[1])
        Number3=int(validate[2])
        Number4=int(validate[3])
        Number5=int(validate[4])
        Number6=int(validate[5])
        Number7=int(validate[6])
        Number8=int(validate[7])
        GTIN=int(Number1*3+Number2+Number3*3+Number4+Number5*3+Number6+Number7*3+Number8)
        if GTIN % 10 == 0:
            print("This GTIN-8 code is valid")
            quantity=int(input("How many of this product would you like to purchase?"))
            order.append([validate,quantity])
            print(order)
        else:
            print("The code you entered is not valid")
    else:
        print("You have not input an 8 digit code")
Reply
#2
Depending on file size, most common approach would be to open the file and read all of its content in memory, in some data structure (most probably - dict, using GTIN code as key). Then you can check if the product number that user has entered is in the dict.
if it is csv file, you may use csv module for convenience.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GTIN code validation program not working! AnonDxZ 1 2,757 Nov-29-2017, 11:25 PM
Last Post: Prrz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020