代码中python 3.4中的对象错误
我不断地发现这个错误:代码中python 3.4中的对象错误,python,Python,我不断地发现这个错误: <__main__.product object at 0x0231A7B0> 它不执行函数prntlist,而是显示该错误 完整代码: class product(object): name = '' price = 0 def __init__(self, name, price): self.name = name self.price = price def prnt(self):
<__main__.product object at 0x0231A7B0>
它不执行函数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")
好的,我让你的例子对我有用,只是为了看看发生了什么:
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
**********************************************************
**********************************************************
项目价格
红色莱斯特13.99
**********************************************************
啊,新鲜出炉。但是我们可以在下周把它运到你家
**********************************************************
fff
(,'/n')
总计$0
**********************************************************
你似乎在问这个:
。正如我在第一条评论中所写的,这是因为当使用下面的行x=I,“/n”
时,您正在生成tuple,而不是字符串。您可以发布实际调用这些方法的代码吗?您发布的内容不是错误…此x=i,“/n”
将x作为元组。不是绳子。不知道这是否导致了您的问题,但从您的代码来看,它应该是str,而不是tuple。prntlist()未被执行。能否提供使用这些类的示例代码,以及如何调用prntlist?如何使prntlist打印列表中的项?我所做的一切似乎都给了我一个错误
**********************************************************
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
**********************************************************