1
即時嘗試從另一個類創建一堆零件對象,它似乎並沒有工作。林正確讀txt文件,這只是這一點,如果能跟大家需要它:不能使用whileloop創建對象(python)
partA 22.50 60
partB 595.00 100
partC 1.95 1000
partD 899.95 100
partE 10.95 100
問題是下#fills部件時,存在的---------------- -----零件庫存類下的註釋。打印語句不會工作。
class Parts:
def __init__(self, partname, price, quantity):
self.partName = partname
self.partPrice = price
self.partQuantity = quantity
def getpartname(self):
return self.partName
def getpartprice(self):
return self.partPrice
def getpartquantity(self):
return self.partQuantity
def setpartname(self, name):
self.partName = name
def setpartprice(self, price):
self.partPrice = price
def setpartquantity(self, quantity):
self.partQuantity = quantity
def equals(self, part2):
if self.partName == part2:
return True
class PartInventory: # constructer, getter setter, dictionary
def __init__(self):
self.partInventoryy={}
partnames = []
partprices = []
partqs = []
partfile = open("parts.txt", "r", encoding="utf-8")
#fills dictionary----------------------------------------------------
for line in partfile:
substring = line.split(' ')
partnames.append(substring[0])
partprices.append(substring[1])
partqs.append(substring[2].strip("\n"))
self.partInventoryy=dict(zip(partnames, partqs))
partPricings = dict(zip(partnames, partprices))
print(self.partInventoryy)
#fills parts--------------------------------------------------------
i=0
while i<len(partnames):
partnames[i]= Parts(partnames[i],partprices[i],partqs[i])
i=i+1
print(partE.getpartname())
#methods---------------------------------------------------------------------
作者:「打印語句不起作用」...你的意思是它不打印出任何東西或拋出異常?我猜測它沒有打印任何東西,你認爲它應該打印出一些東西? –
說NameError:name'partE'沒有定義 – strugglingprogrammer
應該打印出來partE。我知道這個存取方法是多餘的,但它只是一個測試 – strugglingprogrammer