2014-12-04 66 views
-1

我不斷收到此錯誤:對象錯誤代碼

<__main__.product object at 0x0231A7B0> 

從代碼:

def prnt(self): 
     print("\n**********************************************************") 
     print(self.prntlist()) 
     print("Grand total\t\t\t\t$", self.setprice()) 
     print("**********************************************************\n") 

    def prntlist(self): 
     x = '' 
     for i in self.cartlist: 
      x = i, "/n" 
     return x 

,而不是執行功能prntlist它會顯示錯誤

全碼:

class product(object): 
    name = '' 
    price = 0 

    def __init__(self, name, price): 
     self.name = name 
     self.price = price 

    def prnt(self): 
     print("\n**********************************************************") 
     print("Item\t\t\t Price") 
     print(self.name,"............ $", self.price) 
     print("\n**********************************************************") 

    def prntline(self): 
     x = self.name + ".........." + self.price 
     return x 

class cart(object): 
    totalprice = 0 
    cartlist = [] 

    def __init__(self): 
     self.totalprice = totalprice 
     self.cartlist = [] 

    def setprice(self): 
     totprice = self.totalprice 
     for i in self.cartlist: 
      self.totalprice += i.price 
     return totprice 

    def prnt(self): 
     print("\n**********************************************************") 
     print(self.prntlist()) 
     print("Grand total\t\t\t\t$", self.setprice()) 
     print("**********************************************************\n") 

    def prntlinecart(self): 
     print("You have purchased: ", self.prntlist()) 

    def prntlist(self): 
     x = '' 
     for i in self.cartlist: 
      x = i, "/n" 
     return x 

    def additem(self, item): 
     self.cartlist.append(item) 
     print("Ah, fresh out. But we can have it shipped to your house next week") 
+0

你可以發佈實際調用這些方法的代碼嗎?你發佈的內容不是錯誤... – dursk 2014-12-04 02:51:13

+0

這個'x = i,「/ n」'使x成爲一個元組。不是字符串。不知道這是否會導致你的問題,但從你的代碼看來,它應該是str而不是元組。 – Marcin 2014-12-04 02:51:17

+0

prntlist()不會被執行。 – 2014-12-04 02:54:16

回答

0

O K,我做你的榜樣的工作對我來說,只是爲了看看發生了什麼:

class product(object): 
    name = '' 
    price = 0 

    def __init__(self, name, price): 
     self.name = name 
     self.price = price 

    def prnt(self): 
     print("\n**********************************************************") 
     print("Item\t\t\t Price") 
     print(self.name,"............ $", self.price) 
     print("\n**********************************************************") 

    def prntline(self): 
     x = self.name + ".........." + self.price 
     return x 

class cart(object): 
    totalprice = 0 
    cartlist = [] 

    def __init__(self): 
     self.totalprice = 0 
     self.cartlist = [] 

    def setprice(self): 
     totprice = self.totalprice 
     for i in self.cartlist: 
      self.totalprice += i.price 
     return totprice 

    def prnt(self): 
     print("\n**********************************************************") 
     print(self.prntlist()) 
     print("Grand total\t\t\t\t$", self.setprice()) 
     print("**********************************************************\n") 

    def prntlinecart(self): 
     print("You have purchased: ", self.prntlist()) 

    def prntlist(self): 
     x = '' 
     print('fff') 
     for i in self.cartlist: 
      x = i, "/n" 

     return x 

    def additem(self, item): 
     self.cartlist.append(item) 
     print("Ah, fresh out. But we can have it shipped to your house next week") 


mycart =  cart() 

RL = product("Red Leicester", 13.99) 
RL.prnt() 

mycart.additem(RL) 
mycart.prnt() 

輸出是:

********************************************************** 
Item    Price 
Red Leicester ............ $ 13.99 

********************************************************** 
Ah, fresh out. But we can have it shipped to your house next week 

********************************************************** 
fff 
(<__main__.product object at 0x7ffbe52609e8>, '/n') 
Grand total    $ 0 
********************************************************** 

看來,你問一下:<__main__.product object at 0x7ffbe52609e8>。正如我在第一條評論中所寫的那樣,這是因爲你正在製作元組,而不是字符串x = i, "/n"