0
from datetime import datetime

class sms_store:
    store = []
    read = []
def add_new_arrival(self,number,time,text):
    sms_store.read.append(len(sms_store.store))
    sms_store.store.append(("From: {}, Recieved: {}, Msg: {}".format(number,time,text)))
def delete(self,i):
    try:
        del sms_store.store[i]
    except IndexError:
        print("Index is out of range. Cannot delete")
def message_count(self):
    return print("Amt of messages in inbox: {}".format(len(sms_store.store)))
def viewall(self):
    print(sms_store.store)
def get_unread_indexes(self):
    #### ###################################I need help for this method.
def get_message(self,i)
    print(sms_store.store[i])
### tests ####
time = datetime.now().strftime('%H:%M:%S')
my_inbox = sms_store() #instantiate an object 'store' for class
my_inbox.add_new_arrival("12345",time,"Hello how are you?") #instance of store object
my_inbox.add_new_arrival("1111111",time,"BYE BYE BYE")
my_inbox.viewall()
my_inbox.msgcount()

感谢您查看此内容。

这是我需要做的:

my_inbox.add_new_arrival()

添加新消息时,其 has_been_viewed 状态设置为 False。

my_inbox.get_unread_indexes()

返回所有尚未查看的 SMS 消息的索引列表

my_inbox.get_message(i)**

返回 (from_number, time_arrived, text_of_sms) for message[i] 还将其状态更改为“已查看”。如果位置 i 没有消息,则返回 None

请帮助我了解上述方法!?

非常感谢!

4

3 回答 3

1

嗨,我稍微调整了你的代码,我想我之前在“如何像计算机科学家一样思考”中做过这个,希望它对你有用。

from datetime import datetime

class SMS_store:

然后

def __init__(self):
    self.store = []


def __str__(self):
    return ("{0}".format(self))

def add_new_arrival(self, number, time, text ):
    self.store.append(("Read: False", "From: "+number, "Recieved: "+time, "Msg: "+text))

def message_count(self):
    return (len(self.store))

def get_unread_indexes(self):
    result = []
    for (i, v) in  enumerate(self.store):
        if v[0] == "Read: False":
            result.append(i)
    return (result)

def get_message(self, i):
    msg = self.store[i]
    msg = ("Read: True",) + msg[1:]
    self.store[i] = (msg)
    return (self.store[i][1:])

def delete(self, i):
    del self.store[i]

def clear(self):
    self.store = []
于 2016-03-19T20:30:17.313 回答
0

您为什么不向您的班级添加另一个列表,称为未读。更改add_new_arrival为将消息添加为未读。

然后在该get_message方法下将指定的消息从未读移动到已读。

最后,您的get_unread方法仅列出未读列表的索引。

于 2013-04-26T16:37:14.407 回答
0

Python SMS 存储程序使用类和方法 - has_been_viewed 状态

import time
class SMS_store:
    def __init__(self):
        self.inbox = []

    def add_new_arrival(self, from_number, text_of_sms,read_status = False):
        number = str(from_number)
        time_received = time.strftime("%D %T")
        self.inbox.append([time_received, number, text_of_sms, read_status])

    def message_count(self):
        return "There are {0} messages in your Inbox".format(len(self.inbox))

    def get_unread_indexes(self):
        unread = []
        for index, message in enumerate(self.inbox):
            if False in message:
                unread.append(index)
        return "Unread Messages in:", unread

    def get_message(self, index):
        message = self.inbox[index]
        message[3] = "Read"
        return message[ : 3]

    def delete(self, index):
        del self.inbox[index]
        return "Deleted Message", index

    def clear(self):
        self.inbox = []
        return "Empty Inbox"
于 2018-07-31T22:05:49.747 回答