2016-12-01 30 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