class merchandise:
def __init__(self, item, quantity, cost):
self.__item = item
self.__quantity = quantity
self.__cost = cost
def set_item(self, item):
self.__item = item
def set_quantity(self, quantity):
self.__quantity = quantity
def set_cost(self, cost):
self.__cost = cost
def get_item(self):
return self.__item
def get_quantity(self):
return self.__quantity
def get_cost(self):
return self.__cost
def get_inventory_value(self):
return (format(self.__quantity * self.__cost, '.2f'))
def __str__(self):
for i in merchandise:
print (self.__item+',', self.__quantity,'@ $'+(format (self.__cost,'.2f')))
import merchandise
def main(_):
def make_list():
for count in range (1,3):
merchandise.set_item("hammer")
merchandise.set_quantity(10)
merchandise.set_cost(14.95)
print(hammer)
hardware = float (input ('Enter a new quantity for hardware '))
jewelry = float (input ('Enter a new cost for jewelry '))
hammer = merchandise.merchandise()
stuff = make_list()
print (stuff)
main()
我不知道我在做什么错误我得到那里错误没有set_item在商品中。我尝试了很多东西,到目前为止没有任何工作。我在这里的方式,或者是一些愚蠢的东西。调用类函数python
您必须初始化/实例 – Li357
是阶级和主要在单独的文件或单个文件?如果分开,当你'进口商品'时,'商品'是一个模块对象,'merchandise.merchandise'是一个类。 _Class_没有'set_item'方法。 _Class instances_另一方面,确实有'set_item'方法。 –