这是我的编码:int()函数的参数必须是字符串或数字,而不是“元组”
#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)'现在'code'是元组包含2个值:'number'和'eight(number)'https://en.wikibooks.org/wiki/Python_Programming/Tuples –