# Unit 3 Problem solving Homework question 2 #check barcodes #The check digit is calculated as follows: #assign weights of 1, 3, 1, 3, 1 to the five digits #multiply each digit by its weight #add the weighted numbers together #divide the total by 10 #the remainder is the check digit import random def calculateCheckDigit(code): x = code[0] + code[2] + code[4] + (code[1] + code[3])*3 x = x % 10 # % is mod, gives remainder after dividing by 10 return x #end sub #main program totalCorrect = 0 for count in range (0,100): barcode = [] for n in range(0,6): digit = int(random.randint(0,9)) barcode.append(digit) checkdigit = calculateCheckDigit(barcode) # print ("Test barcode and check digit", barcode, checkdigit) if checkdigit == barcode[5]: totalCorrect = totalCorrect + 1 print(barcode) #endif print ("Total correct barcodes ",totalCorrect)