【发布时间】:2015-12-16 12:13:25
【问题描述】:
当我运行这段代码时,shell 输出错误 'Can't convert 'int' object to str implicitly'。
我已尝试对 StackOverflow 进行其他修复,但无法在我的代码中修复该问题。
import math
while True:
try:
di = input('Input 7 digit number ')
total = (int(di[0])+int(di[2])+int(di[4])+int(di[6]))*3+(int(di[1])+int(di[3])+int(di[5]))
if len(str(di)) != 7:
print('Incorrect')
if len(str(di)) == 7:
print('Okay')
multiplier = [3,1]
times = ''
total = 0
for index, digit in enumerate(list(str(di))):
total = total + int(digit)*multiplier[index%2]
times = times+str(int(digit)*multiplier[index%2])+', '
mof10 = total + (10 - total%10)
checkdigit = mof10 - total
final = str(di) + str(checkdigit)
print(times[:-1])
print(total)
print(mof10)
print(checkdigit)
print(final)
final = (di[1]+di[3]+di[5])+((di[0]+di[2]+di[4]+di[6])*3)
final = (final+4)
Base10=(int(round(final, -1)))
Check = Base10-final
checkdigit = int(input('8th number in product code: '))
if (n8 == Check):
print('Valid product code')
except ValueError:
print('Invalid product code')
break
错误信息:
Traceback(最近一次调用最后一次):
文件“C:\Documents and Settings\User\Desktop\GTIN-8 product code.py”,第 55 行,在 final = (final+4)
TypeError: 无法将 'int' 对象隐式转换为 str
【问题讨论】:
-
您能否发布错误消息的完整回溯,而不仅仅是您的错误摘要
-
是的,抱歉忘了这样做:P
-
对不起,我对堆栈溢出还不太熟悉...
标签: python python-3.x