';ValueError:以10为基数的int()的文本无效:';N'';用Python

';ValueError:以10为基数的int()的文本无效:';N'';用Python,python,Python,我创建了一个函数来接收7位代码中的奇数,然后将它们添加到名为“oddDigitList”的列表中,但由于某种原因,我在运行代码时出现了运行时错误 # 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):

我创建了一个函数来接收7位代码中的奇数,然后将它们添加到名为“oddDigitList”的列表中,但由于某种原因,我在运行代码时出现了运行时错误

# 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'

有人能解释一下为什么我的代码是错误的,并提供我函数的正确版本吗。

我猜这就是你想要的:

def formOddDigitList(number):
    """
        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
    """

    oddDigitList = []

    string = str(number)

    # get values for only the odd digits from digit #1 to digit #7

    for i in range(0, 7, 2):
        # The digits in the 'string' variable are added to the 'oddDigitList' list
        # The digits are converted into integers so that they can be used to calculate the product code
        oddDigitList.append(int(string[i]))

    return oddDigitList


print(formOddDigitList(9638507))
返回/打印:

[9, 3, 5, 7]

对于GTIN-8,我假设您希望奇数与常数相乘,然后与偶数相加。所有这些都可以通过一个函数来完成。

感谢大家非常努力地回答我的问题,我确实意识到我确实需要在函数中添加更多内容,以便大家知道函数的上下文是什么。下面是我老师的解决方案,它完全解决了出现的问题:

oddDigitList = []

# 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
    for i in range(0,8,2):
        # 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

# A function used as an input to assign boundaries/extremes to variables as well as enforcing 'try' and 'except' loops so that the user cannot crash the program by inputting a random number

def assignBoundariesToInput(number, text):
    while True:
        try:
            variable = input(text)
        if len(variable) != number:
            raise ValueError
    # This statement keeps looping until the user inputs the suitable data type
    except ValueError:
        print("Please input a/an", number, "digit number")
    else:
        break
    return variable

gtin7OrGtin8 = str(input("Would you the like program to:\n\n a) Calculate the GTIN-8 product code from a seven digit number\n b) Check the validity of an eight digit GTIN-8 code\n c) Exit\n\nPlease enter your designated letter: ")).lower()

if gtin7OrGtin8 == "a":
    gtinput7 = assignBoundariesToInput(7, "Enter the 7-digit code: ")
    formOddDigitList(gtinput7)
    print(oddDigitList)

else:
    pass

正如我之前所说,我非常抱歉没有添加额外的细节来帮助你们解决这个bug。今后,我将确保始终在问题中添加上下文,以帮助社区回答我遇到的任何问题。

查看
变量的内容。如错误所示,其中有一个
N
。另外,了解有关创建的更多信息。在您的情况下,要再现错误,当出现错误时,我们显然需要函数体和函数参数。“变量”是7位整数。您可以提供函数失败的参数值吗?在我的场景中,
variable
等于
1234567