2016-05-16 95 views
-4

我创建了一个函数,接收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' 

是否有人可以解释为什么我的代码是错误的,提供本人功能的一个正确版本。

+5

看'变量'的内容。如错误所示,其中有一个“N”。 – njzk2

+2

另外,了解有关创建[MCVE]的更多信息。在你的情况下,为了重现错误,当发生错误时,我们显然需要函数体**和**函数参数。 –

+0

'变量'是7位整数 –

回答

0

我猜这是你想要什么:

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,我想你想的奇数位数字由一个常数乘以他们并将它们添加到偶数位。所有这些都可以通过单一功能完成。

1

谢谢大家非常难以回答我的问题,我意识到我确实需要添加更多的功能,以便你们知道函数的上下文是为了什么。这里是我的老师的解决方案,从而彻底解决了出现的问题:

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 

正如我以前说过,我不添加额外的细节来帮助你们解决的错误深感抱歉。将来,我会确保始终为问题添加背景信息,以帮助社区解决发生在我身上的任何问题。

相关问题