title: How to resolve Python Attribute Error-'str' object has no attribute 'balance_inventory'
body: <p>My code throws the following errors:</p>
  
  <h3>Error 1:</h3>
  
  <blockquote>
    <p><code>{'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25} iPhone Model X
    AttributeError Traceback (most recent call last) &lt;ipython-input-51-e83a253ecf45&gt; in &lt;module&gt; 50 51 Test_Inventory_Add_Stock.setup_class() ---&gt; 52 Test_Inventory_Add_Stock.test_add_new_stock_as_dict()</code></p>
  </blockquote>
  
  <h3>Error 2:</h3>
  
  <blockquote>
    <p><code>AttributeError: 'str' object has no attribute 'balance_inventory' on Test_Inventory_Add_Stock.test_add_new_stock_as_dict()</code></p>
  </blockquote>
  
  <p>Can anyone please help me on how to resolve this error?</p>
  
  <pre><code>class InsufficientException(Exception):
    pass
  class MobileInventory:
  
      def __init__(self, inventory=None):
          if inventory == None:
              balance_inventory = {}
          elif not isinstance(inventory, dict):
              raise TypeError("Input inventory must be a dictionary")
          elif not (set(map(type, inventory)) == {str}):
              raise ValueError("Mobile model name must be a string")
          elif [True for i in inventory.values() if (not isinstance(i, int) or i &lt; 1)]:
              raise ValueError("No. of mobiles must be a positive integer")
          self.balance_inventory = inventory
  class add_stock():
  
      def __init__(self, m, new_stock):
          if not isinstance(new_stock, dict):
              raise TypeError("Input stock must be a dictionary")
          elif not (set(map(type, new_stock)) == {str}):
              raise ValueError("Mobile model name must be a string")
          elif [True for i in new_stock.values() if (not isinstance(i, int) or i &lt; 1)]:
              raise ValueError("No. of mobiles must be a positive integer")
          for key, value in new_stock.items():
              print(key)
              if key in m.balance_inventory.keys():
                  x = m.balance_inventory[key] + value
                  m.balance_inventory[key] = x
              else:
                  m.balance_inventory.update({key: value})
  class Test_Inventory_Add_Stock:
  
      def setup_class():
          m = MobileInventory({'iPhone Model xy': 100, 'Xiaomi Model YA': 1000, 'Nokia Model Zs': 25})
          print(m.balance_inventory) 
  
      def test_add_new_stock_as_dict():
          o={'iPhone Model X': 50, 'Xiaomi Model Y': 2000, 'Nokia Model A': 10}
          add_stock( m, o)
  Test_Inventory_Add_Stock.setup_class()
  Test_Inventory_Add_Stock.test_add_new_stock_as_dict()
  </code></pre>