Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现类型错误:';str';对象不能解释为整数?_Python_Python 3.x - Fatal编程技术网

Python 为什么会出现类型错误:';str';对象不能解释为整数?

Python 为什么会出现类型错误:';str';对象不能解释为整数?,python,python-3.x,Python,Python 3.x,我在做一个有秩序的练习。我正在尝试使用input().rsplit方法为字典赋值。出现一个名为“TypeError:“str”对象不能解释为整数”的错误。我哪里出错了 从集合导入订单数据 num=输入() itemlist=OrderedDict() 对于范围内的i(num): itemName,itemPrice=input().rsplit(“”,1) 如果itemlist中有itemName: itemlist[itemName]+=int(itemPrice) 其他: itemlist[

我在做一个有秩序的练习。我正在尝试使用input().rsplit方法为字典赋值。出现一个名为“TypeError:“str”对象不能解释为整数”的错误。我哪里出错了

从集合导入订单数据
num=输入()
itemlist=OrderedDict()
对于范围内的i(num):
itemName,itemPrice=input().rsplit(“”,1)
如果itemlist中有itemName:
itemlist[itemName]+=int(itemPrice)
其他:
itemlist[itemName]=int(itemPrice)
itemlist.items()中的名称、价格:
打印str(名称),str(价格)
我希望将用户的输入像“香蕉薯条12”一样存储到字典中

以下是OrderedDict的示例

ordered_dictionary = OrderedDict()
>>> ordered_dictionary['a'] = 1
>>> ordered_dictionary['b'] = 2
>>> ordered_dictionary['c'] = 3
>>> ordered_dictionary['d'] = 4
>>> ordered_dictionary['e'] = 5
>>> 
>>> print ordered_dictionary
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
此错误似乎告诉我无法将itemPrice分配给itemlist[itemName]…我不知道为什么
num=input()
从用户处读取值,并且类型为string。在将其与
范围一起使用之前,必须将其转换为整数

因此,它应该是:

num = int(input())

num=input()
应该是
num=int(input())
。如果不是这样的话,你需要做一个分析,并包括完整的错误消息和回溯。这可能重复回答你的问题吗?