python - 如何删除一元 +: 'str' 的错误错误操作数类型。在我的循环句子中
问题描述
所以我几乎得到了这个代码,因为它只是关于字典。我遇到的唯一问题是错误是一元 + 的操作数类型错误:'str'。
这是我的代码:
express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
for x,y in express_file.items():
print(x,'runs through',+y+ '.')
print('The following Expressway are included in this data set:')
for x in express_file.keys():
print(x)
print('\nThe following Provinces are included in this data set:')
for x in express_file.values():
print(x)
追溯
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-782fcd7b686e> in <module>
1 express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
2 for x,y in express_file.items():
----> 3 print(x,'runs through',+y+ '.')
4 print('The following Expressway are included in this data set:')
5 for x in express_file.keys():
TypeError: bad operand type for unary +: 'str'
解决方案
express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
for x,y in express_file.items():
print(x,'runs through'+y+ '.')
print('The following Expressway are included in this data set:')
for x in express_file.keys():
print(x)
print('\nThe following Provinces are included in this data set:')
for x in express_file.values():
print(x)
您应该删除 +y+ 旁边的逗号。
推荐阅读
- swift - TableViewController 中的函数 loadMeals() 不起作用
- android - 三星中断中的指纹覆盖
- c# - 在字符串中查找前后随机数字的单词
- variables - 使用 var 的每个元素在 Mule 中发出 http 请求
- python - 遍历熊猫中的每个列名后更改列名
- wicket - 如何使用 StringResourceModel 显示带有一个简单参数的简单检票口消息
- angular - 如何隐藏导航 url Ionic 4
- css - 使用固定标题强制 td 与表中的 th 宽度相同
- groovy - 无法更新新链接的问题 - Jira/Scriptrunner - Epic/Epic 中的问题
- python - Python3:导入类问题