我只需要在如何將我加起來只有奇數位置號碼的想法加起來奇數位置的數字。例如,如果我有,我只需要添加0,2,4,6,8等等。我目前擁有的基本上是一個模塊(尚未完成),而且此程序要求我添加valadiate UPC-12數字。我完全困惑,因爲我不完全確定我在做什麼。我還沒有學會「len」(類似那樣)。你怎麼只在UPC-12驗證
# Gets the digit of the number using the specified position
def get_digit(number, position):
return number/(10**position) % 10
def is_UPC12(number):
sum_odd = 0
sum_even = 0
#loops through the UPC code and checks every odd position and adds the numbers
for num in range(1, 13, 2):
sum_odd += get_digit(number, num)
sum_odd *= 3
#loops through the UPC code and checks every even position and adds the numbers
for num in range(2, 13, 2):
sum_of_even += even
sum_even += get_digit(number, num)
Sum = sum_of_odd + sum_of_even_two
#subtracts 10 from the last digit of the sum, and if it's equal to the last digit of number then it returns True.
if 10 - get_digit(Sum , 0) == get_digit(number , 0):
return True
elif 10 - get_digit(Sum , 0) == 10 and get_digit(number , 0) == 0:
return True
else:
return False
基本上,我要創建一個驗證UPC-12號的功能 – kden