2016-05-16 195 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 

正如我以前說過,我不添加額外的細節來幫助你們解決的錯誤深感抱歉。將來,我會確保始終爲問題添加背景信息,以幫助社區解決發生在我身上的任何問題。

相關問題