HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

GTIN validation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gtinvalidationstackoverflow

Problem

I have a school assignment to allow a user to enter a GTIN and then validate that it is correct.

I have finished the code, but my teacher has told me I will get more marks if I can make it more efficient. I realise I could use a loop in lines 5-12, but am not completely sure how. Any feedback is welcome, but I'm really looking for ways to make the code more efficient.

GTIN=''
while True:
    GTIN=(input("please enter GTIN for product"))
    if GTIN.isnumeric() and len(GTIN)==8 and GTIN in open('read_it.txt').read():
        Num0=int(GTIN[0])*3
        Num1=int(GTIN[1])
        Num2=int(GTIN[2])*3
        Num3=int(GTIN[3])
        Num4=int(GTIN[4])*3
        Num5=int(GTIN[5])
        Num6=int(GTIN[6])*3
        Num7=int(GTIN[7])
        total2=(Num0+Num1+Num2+Num3+Num4+Num5+Num6+Num7)
        if total2 % 10 == 0:
            print(GTIN)
        else:
            print("Product Not Found")

Solution


  • You are opening and reading the file every time. Assuming the file doesn't change, this can be done once before the while True loop.



  • You could use a for loop to calculate the value of total2 but each of those calculations still need to happen. So it won't really change the efficiency of the code, it only changes how the logic is written.



You can write the code as follows to make it shorter:

GTIN=''
file_contents = open('read_it.txt').read()
while True:
    GTIN=(input("please enter GTIN for product"))
    if GTIN.isnumeric() and len(GTIN)==8 and GTIN in file_contents:
        total2 = 0
        for i, n in enumerate(GTIN):
            v = int(n)
            if i % 2 == 0:
                v = v * 3
            total2 += v
        if total2 % 10 == 0:
            print(GTIN)
        else:
            print("Product Not Found")


This uses the enumerate() function which allows you to loop over something while counting at the same time. So you'll get the first element of GTIN with the index 0, then the next element of GTIN with index 1 and so on.

The pattern is that if the index value is even (if i % 2 == 0:) then we multiple by 3. Otherwise we don't do that. At each step we add the calculated value to total2 and we then end up with the final value for total2.

If you don't want to use that then you can write it as:

GTIN=''
file_contents = open('read_it.txt').read()
while True:
    GTIN=(input("please enter GTIN for product"))
    if GTIN.isnumeric() and len(GTIN)==8 and GTIN in file_contents:
        total2 = 0
        i = 0
        for n in GTIN:
            v = int(n)
            if i % 2 == 0:
                v = v * 3
            total2 += v
            i += 1
        if total2 % 10 == 0:
            print(GTIN)
        else:
            print("Product Not Found")

Code Snippets

GTIN=''
file_contents = open('read_it.txt').read()
while True:
    GTIN=(input("please enter GTIN for product"))
    if GTIN.isnumeric() and len(GTIN)==8 and GTIN in file_contents:
        total2 = 0
        for i, n in enumerate(GTIN):
            v = int(n)
            if i % 2 == 0:
                v = v * 3
            total2 += v
        if total2 % 10 == 0:
            print(GTIN)
        else:
            print("Product Not Found")
GTIN=''
file_contents = open('read_it.txt').read()
while True:
    GTIN=(input("please enter GTIN for product"))
    if GTIN.isnumeric() and len(GTIN)==8 and GTIN in file_contents:
        total2 = 0
        i = 0
        for n in GTIN:
            v = int(n)
            if i % 2 == 0:
                v = v * 3
            total2 += v
            i += 1
        if total2 % 10 == 0:
            print(GTIN)
        else:
            print("Product Not Found")

Context

StackExchange Code Review Q#142988, answer score: 5

Revisions (0)

No revisions yet.