我在运行时不断遇到上述错误:
def Decode(iList):
IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
,"Master Card","Union Pay","Petroleum"]
TypeList = ["Debit account", "Credit account"]
for istr in iList:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:]
WriteFile(ostr)
File "", line 24, in Decode
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
TypeError: bad operand type for unary +: 'str'
我尝试了str()的错误线路,但没有成功
您有一个有效、完整的Python行
然后,另一行以一元+开头,这不是应用于字符串的有效语句
有关在多行上继续表达式的选项,请参见How can I do a line continuation in Python
在style guide中建议的做法是在运算符之前使用括号和break
假设python知道要将表达式带到下一行。在本例中,它不知道这一点,除非在行尾使用
\
告诉它,或者将表达式括在()
中。基本上以某种方式表示看似完成的表达式不完整,并继续下一行这:
应该是:
或:
现在让我解释一下您得到的实际错误“TypeError:一元+的错误操作数类型:”“str'in string creation”是因为它已经完成了对看似完整的第一行或上面删掉的部分的解析,现在又开始解析第二行。它看到新行以
+
开头,因此它必须是一元数+
,因为+
的左边没有操作数。它不适用于下一个字符串操作数,因此会出现问题在多行上创建字符串时添加反斜杠可以解决以下问题:
相关问题 更多 >
编程相关推荐