我創建了一個函數,接收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位整數 –