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'方法。 –