Hey all. First off: not looking for answers as this is for a Uni assignment. I’m wondering if somebody can point me in the right direction. The following program is supposed to:
- accept a user input
- determine if that user input is a valid basic code, positional code, or upc code, based on:
Basic Code :
- Add all digits (except the last one) together
- compute sum % 10 (between 0-9)
- add that digit (from step 2) to the end of the digit. If that last digit is correct ,the number is a valid Basic Code.
ie. for 434135:
4 + 3 + 4 + 1 + 3 = 15
15 % 5 = 5
Last digit of the number is a “5”, so the number is a valid Basic Code.
Positional Code :
- Compute the sum by multiplying each digit (except the last one) by it’s position in the string
- Compute the result % 10
- Add that number to the end of the digit
ie. 434135:
(4 * 1) + (3 * 2) + (4 * 3) + (1 * 4) + (3 * 5) = 41
41 % 10 = 1
Last digit of the number is not 1, so this code is not a valid positional code
UPC :
- multiply each odd digit by 3 and each even digit by 1 and add together
- compute the result % 10
- If resulting number is a 0, add it to the end. If the resulting number is 1-9, subtract that number from 10 to get the final digit.
ie. 434135:
(4 * 1) + (3 * 3) + (4 * 1) + (1 * 3) + (3 * 3) = 29
29 % 10 = 9
10 - 9 = 1
Last digit is not a 1, therefore this is not a valid UPC code.
We have to create two separate programs. The first one houses our functions and the second one is the main program that calls the functions. I’m not sure what I’m doing wrong, but every number I enter (regardless of whether or not it should be a valid Basic Number, Positional Number, or UPC Number) is ending up in the “invalid” list and printing out with “None:” instead. Any advice?
first file (contains functions):
''' This program contains the functions needed for assign2.py. These functions
check to see if an inputted code is a valid basic code, positional code, or UPC
code. '''
# basicCode function takes input, converts to integer, and adds all digits together
# before determining if the check digit is valid.
def basicCode(code) :
inputList = list(map(int, code.strip()))
total = sum(inputList)
checkDigit = total % 10
finalDigit = code[-1]
if checkDigit == finalDigit :
return True
# positionalCode function takes input, converts to integer, and multiplies
# each digit by its position and adds the result to the total sum before
# determining if the check digit is valid.
def positionalCode(code) :
inputList = list(map(int, code.strip()))
for i in range (0, len(inputList)) :
total = 0
total +=(i + 1) * inputList[i]
checkDigit = total % 10
finalDigit = code[-1]
if checkDigit == finalDigit :
return True
# UPC function takes input, converts to integer, and multiplies each odd digit by
# 3 and leaves each even digit alone (multiplies by 1). It then subtracts any
# numbers between 1-9 from 10 to determine the checkDigit or (if the number is 0)
# determines that the checkDigit is 0. The function then verifies the code.
def UPC(code) :
total = 0
inputList = list(map(int, code.strip()))
for i in range (0, len(inputList)) :
if i % 2 == 0 :
total = total + (3 * inputList[i])
else :
total += inputList[i]
checkDigit = total % 10
if checkDigit == 0 :
checkDigit = 10 - checkDigit
finalDigit = code[-1]
if checkDigit == finalDigit :
return True
Second code (main program):
from check_code import *
inputList =[]
basic = []
positional = []
upc = []
invalid = []
while True :
code = input("Please enter a code. To exit, enter 0. ")
if code == "0" :
break
inputList.append(code)
for c in inputList :
if basicCode(c) == True :
print("--code: " + c + "valid Basic code.")
basic.append(c)
if positionalCode(c) == True :
print("--code: " + c + "valid Position code.")
positional.append(c)
if UPC(c) == True :
print("--code: " + c + "valid UPC code.")
upc.append(c)
if not basicCode == True and not positionalCode == True and not UPC == True :
print("--code: " + c + "not Basic, Position or UPC code.")
invalid.append(c)
print("Summary")
print("Basic :" + ", ".join(basic))
print("Position :" + ", ".join(positional))
print("UPC :" + ", ".join(upc))
print("None :" + ", ".join(invalid))