A class with no contructor (Python) -
given need operate machine, need vendingmachine class:
property stock(list) stores food items.
methods:
constructor takes in no arguments.
get_stock_names(): returns list of strings represents names of food items in stock. load(food): adds food object stock
and others,
#predefined class food(object): def __init__(self, name, nutrition, good_until): self.name = name self.nutrition = nutrition self.good_until = good_until self.age = 0 def get_name(self): return str(self.name) def get_age(self): return self.age def get_nutrition(self): if self.age <= self.good_until: return self.nutrition else: return 0 def ripen(self, days): self.age = self.age + days return self.age def is_spoiled(self): return self.good_until < self.age #my code below class vendingmachine: def __init__(self): property = food.get_name #no clue how make property self.load = food.load #same here def get_stock_names(self, property): lst = [] in food: = str(i) lst.append(i) return lst def has_stock(self, name): return name in property def load(self, food): property.append(food) return property def sell(self, name): if name in property: property.remove(name) return name else: return none
what is
attributeerror: 'vendingmachine' object has no attribute 'load' (a variable)
i'm pretty sure you've misunderstood line of instructions telling stock
property. suspect telling make instance variable named self.stock
holds list of food
instances. since constructor takes no arguments, presumably starts empty. using term "property" seems red herring, since property
has specific meaning in python (a wrapper around method make attribute), doesn't make sense in situation.
anyway, here's think want constructor like:
def vendingmachine(object): def __init__(self): self.stock = [] # empty
your later methods can inspect or manipulate self.stock
necessary.
Comments
Post a Comment