Clink
Clink

Reputation: 3

How to solve TypeError: 'int' object is not iterable in my GTIN code

I am creating a code that checks the 8th digit of a 7 digit GTIN 8 number and then tells you if it is a vaild GTIN number. But I get this message and do not know what to do to fix it.

How should I alter my code to stop this problem and have the functions of the code work? Thanks

CODE:

while 2>1:
GTIN = input("Enter 7 digit number for check-digit. Enter 8 digit number for validity.")
if not GTIN.isdigit() or not len(GTIN) == 7:
    print("invalid entry...")
    continue

a = int(GTIN[0])*3
b = int(GTIN[1])*1
c = int(GTIN[2])*3
d = int(GTIN[3])*1
e = int(GTIN[4])*3
f = int(GTIN[5])*1
g = int(GTIN[6])*3

total = (a+b+c+d+e+f+g)

checkdigit = (total + 9) // 10 * 10 - total

if len(GTIN) == 7:
    print("Your check digit is",checkdigit)

if sum(total)%10==0:
    print("Valid GTIN-8 number")
else:
    print("Invalid GTIN number")

ERROR MESSAGE:

if sum(total)%10==0: TypeError: 'int' object is not iterable

Upvotes: 0

Views: 86

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55630

The exception occurs because sum expects a sequence (for example a list of numbers) as its argument, but you have passed it a single integer (or int), total. An int is not composed of other objects, so you cannot iterate over it like you can with a list or set, for example, hence the TypeError.

total is already the sum of a, b, c, d, e, f, and g, so you don't need to call sum on it.

Just do

if total % 10 == 0:

Upvotes: 2

Related Questions