python - Missing 1 required positional argument (object created and dictionary initialized) -
i see question has been asked before in different variations, feel though have implemented feedback have seen in threads (mainly making sure object created have done in second last night, , making sure dictionary initiated, feel have done in third line) , still receiving error. advice appreciated. thank you!
class groceries: def __init__(self, grocery_list): self.grocery_list = {} def add_item(self, item): item = input("name: ") purchased = input(false) self.grocery_list[item] = purchased = groceries() something.add_item()
the error is:
traceback (most recent call last): file "intermediate_python.py", line 14, in <module> = groceries() typeerror: __init__() missing 1 required positional argument: 'grocery_list'
i tried resolve error removing grocery_list def init statement (not sure why work, playing around) , error moved next line with:
traceback (most recent call last): file "intermediate_python.py", line 15, in <module> something.add_item() typeerror: add_item() missing 1 required positional argument: 'item'
to initialise instance of groceries class need provide parameter "grocery_list" defined in init method
class groceries: def __init__(self, grocery_list): self.grocery_list = {}
try create instance using following:
something = groceries({"milk": 1, "butter": 1})
alternatively, allow empty groceries objects, can add default parameter init method, should assigning value of parameter variable self.grocery_list instead of assigning empty dictionary:
class groceries: def __init__(self, grocery_list={}): self.grocery_list = grocery_list
and method add_item, again need pass parameter or else removed parameter (since you're asking input)
def add_item(self): item = input("name: ") purchased = input("amount: ") self.grocery_list[item] = purchased = groceries({"milk": 0}) something.add_item() >> name: >>> milk >> amount: >>> 1
Comments
Post a Comment