2017-02-21 15 views
0

私は多くの方法を試してみましたが、私のクラス私のコードでメソッドを介して私のリストの長さを得るように見えることはできません。「はAttributeError: 『リスト』オブジェクトが属性を持っていない 『SMSMessage』」

SMSStore = [] 
unreadMessage = [] 
class SMSMessage(object): 

    def __init__(self, hasBeenRead, messageText, fromNumber): 
     self.hasBeenRead = hasBeenRead 
     self.messageText = messageText 
     self.fromNumber = fromNumber 
     hasBeenRead = False 

    def markAsRead(self, hasBeenRead): 
     hasBeenRead = True 

    def add_sms(self): 
     newMessage = (self.hasBeenRead, self.messageText, self.fromNumber) 
     return SMSStore.append(newMessage) 

    def get_count(): 
     return len(SMSStore) 

    def get_message(self, i): 
     hasBeenRead = True 
     return SMSStore[i][1] 

    def get_unread_messages(i): 
     for i in SMSStore: 
      if SMSStore[i][0] == False: 
       unreadMessage.append(SMSStore[i]) 
     print unreadMessage 

    def remove(self, i): 
     return SMSStore.remove(i)  

#sample = SMSMessage(False, "Hello friend!", 0742017560) 

そして、ここでクラスは、私はちょうど私達でき

userChoice = "" 

while userChoice != "quit": 
    userChoice = raw_input("What would you like to do - read/send/quit?") 
    if userChoice == "read": 
     print len(SMSStore)#this way i can get the length of the list anyway without using get_count 
     SMSStore(get_count() 

     unreadChoice = raw_input("Would you like to retrieve all unread messages or one of your own choice? - all unread/custom ") 
     if unreadChoice == "custom":     
      i = int(raw_input("Please enter which message number you want to read: ")) 
      print get_message(i) #I dont understand how i works and how to get it working with the object definition 

    elif userChoice == "send": 
     messageText = raw_input("Please type in your message: ") 
     fromNumber = raw_input("Please type in the number it was sent from ") 
     newObject = SMSMessage(False, messageText, fromNumber) 
     newObject.add_sms() 

     print SMSStore 

    elif userChoice == "quit": 
     print "Goodbye" 

    else: 
     print "Oops - incorrect input" 

を使用する方法です。これは、リスト内のメッセージは、理想的には、次のようになります方法ですe len(SMSStore)しかし、私はそれを取得するために、クラス内のメソッドを使用できるようにしたい。何か間違いを指摘できますか?

これは尋ねた質問だった:あなたのスクリプトで

Open the file called​  sms.py​ 
Create a class definition for an SMSMessage which has three variables: 
    hasBeenRead, messageText, and fromNumber.  
The constructor should initialise the sender’s number.  
The constructor should also initialise hasBeenRead  to false 
Create a method in this class called MarkAsRead which should change hasBeenRead to true. 
Create a list called SMSStore to be used as the inbox. 

Then create the following methods: 
add_sms - which takes in the text and number from the received sms to 
      make a new SMSMessage object.  
get_count - returns the number of messages in the store. 
get_message - returns the text of a message in the list.Forthis, allow the 
       user to input an index i.e. GetMessage(i) returns the message 
       stored at position i in the list. Once this has been done, 
       hasBeenRead should now be true.  
get_unread_messages - should return a list of all the messages which 
         haven’t been read.  
remove - removes a message in the SMSStore.  
Now that you have these set up, let’s get everything working!  
+0

これが問題である、それを呼び出す:三つの変数があるSMSMessageのためのクラス定義の作成● –

+0

●オープンファイルsms.py –

+0

呼ば:hasBeenRead、messageTextを、そしてfromNumber。 –

答えて

0
あなたSMSMessageクラスで

def get_count(self, *args): 
    return len(SMSStore) 

SMSStoreがグローバル変数である

# create instance 
sms_msg = SMSMessage() # init arg needed 
print sms_msg.get_count() 

、あなたがSMSMessageからget_countを削除することができスコープ

SMSStore = [] 
unreadMessage = [] 
class SMSMessage(object): 
    ...functions... 

def get_count(*args): 
    return len(SMSStore) 

と定期的OR

ls = range(20) 

class A(object): 
    # declare default value for your arguments 
    def __init__(self, hasBeenRead = False, messageText = "", fromNumber=0): 
    self.a = a 
    self.b = b 
    #using classmethod 
    @classmethod 
    def get_count(cls, *args): 
    return len(ls) 


print A.get_count() 
+0

Thanks、「TypeError:__init __()は4つの引数(1が指定されています)を受け取る」というエラーが表示されます –

+0

私はそれを理解していますが、彼らが言ったように、私は質問を投稿することができますか? –

+0

なぜあなたの質問に追加してください... –

関連する問題