2016-12-01 31 views
1
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'对象没有属性'name'

AttributeError: 'str' object has no attribute 'name' 
+0

请勿更改原始码。保持它的错误,否则任何人通过这将与所提供的答案混淆。 –

回答

0

前两个错误是在此功能__str__(self) 它应该是'\tName: ' + self.name + '\tPrice: ' (您已经错过了+标志,没有包括单引号里面\ t。)

return 'Code: ' + self.code + '\tName: ' + self.name + '\tPrice: ' + self.price 

然后在choose_item(self)功能。 (Missing + sign)

print str(count) + '\t' + item.name + '\t' + item.cost 
+0

谢谢@ MarlonAbeykoon我改变了代码,仍然给我同样的错误。任何建议修复代码? – Beginner

+0

我更新了答案'\ tPrice:'也进行了更正。 –

+0

我修复了这个问题,但仍然给我同样的错误。 – Beginner