简体   繁体   English

无法修复 Python 错误 AttributeError: 'int' object 没有属性 'get'

[英]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先感谢您

  1. get should be called on the dictionary (ie inventory ). get应该在字典上调用(即inventory )。
  2. There is no need to call get because v = inventory[k] in the loop.无需调用get因为v = inventory[k]在循环中。
  3. You do not need to have a default in case 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))的其他反馈,您可以尝试以下操作:

  1. Use + notation print(str(k) + ' ' + str(v)) which allows only string使用+表示法print(str(k) + ' ' + str(v))允许string
  2. Or use , notation print(k, v) which allows a mix of string and list或者使用允许stringlist混合,符号print(k, v)
  3. Use , 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.

相关问题 如何修复&#39;AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;get&#39;错误 - How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python AttributeError: &#39;int&#39; 对象没有属性 &#39;get&#39; 我该如何修复它 - AttributeError: 'int' object has no attribute 'get' how can i fix it 如何在 Python 中修复此错误(AttributeError: &#39;Int64Index&#39; object has no attribute &#39;dayofweek&#39; - How to fix this error in Python ( AttributeError: 'Int64Index' object has no attribute 'dayofweek' python celery AttributeError: &#39;int&#39; 对象没有属性 &#39;upper&#39; 错误 - python celery AttributeError: 'int' object has no attribute 'upper' error Python错误:AttributeError:&#39;int&#39;对象没有属性&#39;append&#39; - Python error: AttributeError: 'int' object has no attribute 'append' 如何修复“列表”object 在 Python/Django 中没有属性“get”(AttributeError) - How to fix 'list' object has no attribute 'get' (AttributeError) in Python/Django 如何修复“AttributeError: &#39;str&#39; object has no attribute &#39;content&#39;”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error “如何解决&#39;AttributeError:&#39;NoneType&#39;对象在Python中没有属性&#39;tbody&#39;错误? - "How to fix 'AttributeError: 'NoneType' object has no attribute 'tbody'' error in Python? python django:如何修复错误说:AttributeError at / &#39;Home&#39; object has no attribute &#39;get&#39; - python django: How to fix error saying: AttributeError at / 'Home' object has no attribute 'get' Python:AttributeError:&#39;int&#39;对象没有属性&#39;append&#39; - Python : AttributeError: 'int' object has no attribute 'append'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM