class System:
def __init__(self,code,name,price):
self.name = name
self.price = price
self.code = code
def __str__(self):
return 'Code: ' + self.code + '\tName: ' self.name + \'tPrice: ' + self.price
def choose_item(self):
count = 0
for item in self.name:
print str(count) + '\t' item.name + '\t' + item.cost
count += 1
question = raw_input('Enter the code: ')
if question == 0:
exit()
elif choice != self.code:
print 'Invalid code'
else:
index = question -1
name[index].self.choose_item()
print 'Your item has been added'
我收到此错误,看不到错误。我想通过键入代码来选择项目,以便添加项目。不确定这是正确的方法。
AttributeError: 'str' object has no attribute 'name'
前两个错误在此函数中__str__(self)
应该是'\tName: ' + self.name + '\tPrice: '
(您错过了+
符号,并且没有在单引号中包含\ t。)
return 'Code: ' + self.code + '\tName: ' + self.name + '\tPrice: ' + self.price
然后在choose_item(self)
功能上。(缺少+号)
print str(count) + '\t' + item.name + '\t' + item.cost
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句