[英]Cant fix Python error AttributeError: 'int' object has no attribute 'get'
My source code我的源代码
def display_inventory(inventory):
print("Itme list")
item_total = 0
for k,v in inventory.items():
print(str(k) + str(v))
item_total = item_total + v.get(k,0)
print("The total number of items:" + str(item_total))
stuff = {'rope':1, 'torch':6, 'coin':42, 'Shuriken':1, 'arrow':12}
display_inventory(stuff)
error message错误信息
AttributeError: 'int' object has no attribute 'get'
Could you please tell me how I can fix this error?你能告诉我如何解决这个错误吗? I'd appreciate if you could also explain why this doesn't work.如果您也能解释为什么这不起作用,我将不胜感激。
thank you in advance先感谢您
get
should be called on the dictionary (ie inventory
). get
应该在字典上调用(即inventory
)。get
because v = inventory[k]
in the loop.无需调用get
因为v = inventory[k]
在循环中。k
does not exist because for k,v in inventory.items()
only loops through existing items.如果k
不存在,则不需要设置默认值,因为for k,v in inventory.items()
仅循环通过现有项目。Why don't you simply write the following:您为什么不简单地编写以下内容:
def display_inventory(inventory):
print("Itme list")
item_total = 0
for k,v in inventory.items():
print(str(k) + str(v))
item_total = item_total + v # <<< CHANGE HERE
print("The total number of items:" + str(item_total))
stuff = {'rope':1, 'torch':6, 'coin':42, 'Shuriken':1, 'arrow':12}
display_inventory(stuff)
Because in this cause the value v
is the number corresponding to the product which you want to add up.因为在这种情况下,值v
是与您要加起来的产品相对应的数字。 There is no need to make any further dictionary lookups using get()
because you already have the value you need.无需使用get()
进行任何进一步的字典查找,因为您已经拥有所需的值。
In your dictionary, v
is an integer.在您的字典中, v
是 integer。 Everything in the dictionary is fetched in inventory.items()
.And you are trying to fetch something from an intege.字典中的所有内容都在inventory.items()
中获取。而您正试图从整数中获取一些东西。 Hence it shows an error.因此它显示一个错误。 The solution is simply change to v
解决方案只是更改为v
def display_inventory(inventory: dict):
print("Item list")
item_total = 0
for k,v in inventory.items():
print(str(k) + str(v))
item_total = item_total + v #=== Here
print("The total number of items:" + str(item_total))
stuff = {'rope':1, 'torch':6, 'coin':42, 'Shuriken':1, 'arrow':12}
display_inventory(stuff)
And the output:和 output:
rope1
torch6
coin42
Shuriken1
arrow12
The total number of items:62
The bug is in the line with v.get(k,0)
.该错误与v.get(k,0)
。 The syntax for .get()
function is .get()
function 的语法是
<dict>.get(<key>, <default value if key not found>
and outputs a value in the dictionary, <dict>.get(<key>, <default value if key not found>
并在字典中输出一个值,
whereas the usual <dict>[<key>]
method would throw an error if the key is not present.而如果键不存在,通常的<dict>[<key>]
方法会抛出错误。
Specific to your code when iterating through the key-value pair for k,v in inventory.items():
, the key is guaranteed to be present.特定于您的代码在迭代for k,v in inventory.items():
的键值对时,该键保证存在。
And since you are already iterating through the values in the dictionary, so you could simply use而且由于您已经在遍历字典中的值,所以您可以简单地使用
item_total += v
. item_total += v
。
Additional feedback on print(str(k) + str(v))
, you could try the following:关于print(str(k) + str(v))
的其他反馈,您可以尝试以下操作:
+
notation print(str(k) + ' ' + str(v))
which allows only string
使用+
表示法print(str(k) + ' ' + str(v))
只允许string
,
notation print(k, v)
which allows a mix of string
and list
或者使用允许string
和list
混合,
符号print(k, v)
,
notation in print("The total number of items:", item_total)
在print("The total number of items:", item_total)
中使用,
表示法Output Output
rope 1
torch 6
coin 42
Shuriken 1
arrow 12
The total number of items: 62
By the way there is a typo in print("Itme list")
.顺便说一句print("Itme list")
有错别字。
Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.