Reputation: 33
I have products inventory program and with modify products function in menu file
def modify_product(self):
id = input("Enter product id: ")
type = input("Enter product type: ")
price = input("Enter product price: ")
quantity = input("Enter product quantity: ")
description = input("Enter product description: ")
if type:
self.inventor.modify_type(id, type)
if price:
self.inventor.modify_price(id, price)
if quantity:
self.inventor.modify_quantity(id, quantity)
if description:
self.inventor.modify_description(id, description)
I am getting error:AttributeError: 'NoneType' object has no attribute 'type'
Here is my modify_type,price,quantity,description functions in file inventor.py:
def modify_type(self, product_id, type=''):
self._find_product(product_id).type = type
def modify_price(self, product_id, price):
self._find_product(product.id).price = price
def modify_quantity(self, product_id, quantity):
self._find_product(product.id).quantity = quantity
def modify_description(self, product_id, quantity):
self._find_product(product.id).description = description
Here is _find_product function:
def _find_product(self, product_id):
for product in self.products:
if str(product.id) ==(product.id):
return product
return None
Upvotes: 0
Views: 4761
Reputation: 1124758
Your self._find_product()
call is returning None
, because you are not testing the correct value in the loop.
Don't test str(product.id) against
product.idbut against the
product_id` argument:
if str(product.id) == product_id:
You are also returning None
too early. That return
statement is redundant, just remove it. If a function ends without a return
, None
is returned by default:
def _find_product(self, product_id):
for product in self.products:
if str(product.id) == product_id:
return product
This can be collapsed into:
def _find_product(self, product_id):
return next((p for p in self.products if str(p.id) == product_id), None)
Upvotes: 1