我正在处理我正在处理的程序问题。它是GTIN-8代码生成器。当我尝试启动程序时,我收到错误:
Type Error: Can't convert 'int' to str implicitly.
我的代码如下:
sevenNum = ""
gtinNum = ""
checkDigit = ""
total = ""
a = ""
b = ""
c = ""
d = ""
e = ""
f = ""
g = ""
def GTINCalc():
a = int(sevenNum[0])*3
b = int(sevenNum[1])*1
c = int(sevenNum[0])*3
d = int(sevenNum[1])*1
e = int(sevenNum[0])*3
f = int(sevenNum[1])*1
g = int(sevenNum[0])*3
total = (a+b+c+d+e+f+g)
checkDigit = (total + 9) // 10 * 10 - total
print("GTIN-8 Code:" + a+b+c+d+e+f+g+checkDigit)
def sevenNumAsk():
sevenNum = input("Enter a 7 digit number to be converted into a GTIN-8 Number")
if sevenNum.isdigit() == True and len(sevenNum) == 7:
print("Valid Number - Calculating GTIN-8...")
GTINCalc()
else:
print("The number is not valid - please re-enter ")
sevenNumAsk()
sevenNumAsk()
我遇到这个部分的问题:
total = (a+b+c+d+e+f+g)
checkDigit = (total + 9) // 10 * 10 - total
非常感谢。
答案 0 :(得分:0)
您无法将string
和int
连接在一起:
print("GTIN-8 Code:" + a+b+c+d+e+f+g+checkDigit)
无论如何使用字符串格式更为正确,例如:
print("GTIN-8 Code: {0}{1}{2}{3}{4}{5}{6}{7}".format(a, b, c, d, e, f, g, checkDigit))
答案 1 :(得分:0)
a
,b
.... g
定义为字符串:
a = ""
b = ""
c = ""
d = ""
e = ""
f = ""
g = ""
并且,当你总结total = (a+b+c+d+e+f+g)
时,total
也是一个字符串。
我认为你的缩进在代码中没有了。如果仅在发布问题时只是复制/粘贴错误,那么您可以参考@Selcuk的答案。