【发布时间】:2017-04-15 22:15:50
【问题描述】:
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'
【问题讨论】:
-
不要更改原代码。保持错误,否则任何经历过这个的人都会对所提供的答案感到困惑。
标签: string python-2.7 object attributeerror