Python 无法找出此代码的错误
这是我代码的一部分,我不断收到错误消息说 回溯(最近一次呼叫最后一次): 文件“F:\python1\python1.py”,第14行,在 A=int(gtin8code[0])*3 TypeError:“int”对象没有属性“getitem” 谁能告诉我怎么了Python 无法找出此代码的错误,python,Python,这是我代码的一部分,我不断收到错误消息说 回溯(最近一次呼叫最后一次): 文件“F:\python1\python1.py”,第14行,在 A=int(gtin8code[0])*3 TypeError:“int”对象没有属性“getitem” 谁能告诉我怎么了 gtin8code = int(input("Please enter your gtin-8 barcode")) A = int(gtin8code[0])*3 B = int(gtin8code[1]) C = int(gtin8
gtin8code = int(input("Please enter your gtin-8 barcode"))
A = int(gtin8code[0])*3
B = int(gtin8code[1])
C = int(gtin8code[2])*3
D = int(gtin8code[3])
E = int(gtin8code[4])*3
F = int(gtin8code[5])
G = int(gtin8code[6])*3
total = (A+B+C+D+E+F+G)
#print(total)
我是初学者,所以请不要粗鲁。您需要将条形码作为字符串输入,因为您将每个字符分别视为
int
:
gtin8code = input("Please enter your gtin-8 barcode")
错误很明显,不是吗:
gtin8code
已经是一个int
。因此,您无法获取gtin8code[0]
…Int类型没有\uu getitem\uu
。将第一行上的强制转换更改为str
,那么我要更改什么?@ScopoPhobia您删除了input
行上的int
cal。@ScopoPhobia真的吗?它对我有用。。。除非您使用的是Python2.x,否则您必须使用raw\u input()
。我认为这就是问题所在,因为它适用于Python3,而不是Python2.7.11。谢谢