user7673862
user7673862

Reputation:

Validate 8 digit GTIN barcode

So I need to write a program which gives the user two options: A) Validate GTIN code or B) Find 8 digit of a GTIN code from a 7 GTIN code. To find out the 8th digit of a GTIN code from a 7 digit GTIN code each digit in the 7 GTIN code must be multiplied in order like this

enter image description here

Then subtract the sum from 50 to get the 8th digit. I am however struggling to make the code, whenever I press B or b instead of displaying the results I want it displays "for i in gtin: NameError: name 'gtin' is not defined" Here is the code

question = input("Would you like to A)Validate your code or B) Find the 8th digit?")
if question in "Aa":
    barcode = input("Please enter your 8 digit number")
counter = 0
sum = 0
for i in barcode:
    counter = counter + 1
    if counter % 2 != 0:
        sum = sum + int(i) * 3
    else:
        sum = sum + int(i) * 1  
if sum % 10 == 0:
    print("Valid GTIN")
else:
    print("Invalid GTIN")

if question in "Bb":
    gtin = input("Enter your 7 digit number")
for i in gtin:
    counter = counter + 1
    if counter % 2 != 0:
        sum = sum + int(i) * 3
    else:
        sum = sum + int(i) * 1

print(sum)
lastdigit = 50 - sum
print("8th digit ", lastdigit)

print("Full 7 digit number ", gtin)
print("Full 8 digit number ", str(gtin+str(lastdigit)) )

Also when I press A or a it gives me the results I want but also displays the message "for i in gtin: NameError: name 'gtin' is not defined"

enter image description here

I do not want this message to be displayed

Upvotes: 0

Views: 728

Answers (1)

The4thIceman
The4thIceman

Reputation: 3889

It breaks because it doesn't know what barcode is because it hasn't been set to anything because it is blocked waiting for user input for an option they didn't choose.

try this:

question = input("Hellp, would you like to A) Validate you barcode or B)Find out the 8th digit number?")
if question in "Bb":
    # do B stuff
elif question in "Aa":
    # do A stuff

Upvotes: 0

Related Questions