这是我的代码:
#Getting them to import their code:
number = int(input("Enter 7 digit GTIN code to get eighth number : "))
#importing math for subtracting later:
import math
#Getting the numbers X3 & X1 and then adding them:
def eight(total):
multiplier = [3, 1]
total = 0
for i, digit in enumerate(str(number)):
total = total + int(digit)*multiplier[i%2]
#Subtracting the total to get the last number:
nearest_10 = int(math.ceil(total / 10.0)) * 10
return nearest_10 - total
code = number,eight(number)
code = int(code)
print(code)
#printing their full number:
#Checking the validity of the eight digit GTIN-8 code:
def validity(valid):
multiplier = [3, 1]
valid = 0
string = ""
for i, digit in enumerate(list(str(code))):
valid = valid + str(digit)*multiplier[i%2]
string = string+str(str(digit)*multiplier[i%2])+", "
if code % 10 == 0:
print"Valid"
else:
print"Not valid"
但是,当我稍后尝试将我的代码转换为整数时,因为答案需要是单个整数,它会这样说:
code = int(code)
TypeError: int()
argument must be a string or a number, not 'tuple'
请您参考如下方法:
你的线路
code = number,eight(number)
使代码成为二进制元组,(number, eight(number)) . Python 添加括号并生成一个元组,因为它经常在幕后这样做以允许更漂亮的代码。然后您的下一行尝试使用 int()其中,这是不允许的。
我不知道你想要什么 eight(number) , 但不清楚你为什么要尝试 int()在这里,因为两者都是number和 eight(number)似乎已经是整数了。你想用那条线做什么?
