我创建了一个函数,接收7位数字代码中的奇数,然后将它们添加到名为'oddDigitList'的列表中,但对于某些原因我在运行代码时遇到运行时错误。'ValueError:无效文字为int()与基数10:'N''在Python
# A function used to add odd digits from a variable to the 'oddDigitList' list, which will be used to calculate the 8th digit of the GTIN-8 code
def formOddDigitList(variable):
# The 'for' loop gets values from digit #1 to digit #7 in twos
variable = str(variable)
for i in range(0,8,2):
variable[i]
# The digits in the 'variable' argument are added to the 'oddDigitList' list
# The digits are converted into integers so that they can be used to calculate the 8th digit of the GTIN-8 product code)
oddDigitList.append(int(variable[i]))
return variable
这里是错误消息我:
oddDigitList.append(int(variable[i]))
ValueError: invalid literal for int() with base 10: 'N'
是否有人可以解释为什么我的代码是错误的,提供本人功能的一个正确版本。
看'变量'的内容。如错误所示,其中有一个“N”。 – njzk2
另外,了解有关创建[MCVE]的更多信息。在你的情况下,为了重现错误,当发生错误时,我们显然需要函数体**和**函数参数。 –
'变量'是7位整数 –