Python实现可以输入事件,并且赋予事件重要程度,按照事件重要程度存储事件,删除事件,修改事件,制定事件完成时间的记事本的代码最后生成txt
时间: 2024-03-18 19:42:14 浏览: 67
好的,我可以为您编写这个记事本的代码。以下是Python实现可以输入事件,并且赋予事件重要程度,按照事件重要程度存储事件,删除事件,修改事件,制定事件完成时间的记事本的代码:
```python
import datetime
class Event:
def __init__(self, name, importance, deadline=None):
self.name = name
self.importance = importance
self.deadline = deadline
def __repr__(self):
return f"{self.name} (Importance: {self.importance}, Deadline: {self.deadline})"
class Notes:
def __init__(self):
self.events = []
def add_event(self, name, importance, deadline=None):
event = Event(name, importance, deadline)
self.events.append(event)
self.sort_events()
def delete_event(self, index):
self.events.pop(index)
def modify_event(self, index, name=None, importance=None, deadline=None):
event = self.events[index]
if name:
event.name = name
if importance:
event.importance = importance
if deadline:
event.deadline = deadline
self.sort_events()
def sort_events(self):
self.events.sort(key=lambda x: x.importance, reverse=True)
def print_events(self):
for i, event in enumerate(self.events):
print(f"{i+1}. {event}")
def save_to_file(self, filename):
with open(filename, "w") as f:
for event in self.events:
deadline_str = event.deadline.strftime("%Y-%m-%d") if event.deadline else ""
f.write(f"{event.name},{event.importance},{deadline_str}\n")
def load_from_file(self, filename):
with open(filename, "r") as f:
for line in f:
line = line.strip()
if line:
name, importance, deadline_str = line.split(",")
importance = int(importance)
deadline = datetime.datetime.strptime(deadline_str, "%Y-%m-%d") if deadline_str else None
self.add_event(name, importance, deadline)
notes = Notes()
while True:
print("1. Add Event")
print("2. Delete Event")
print("3. Modify Event")
print("4. Print Events")
print("5. Save to File")
print("6. Load from File")
print("7. Exit")
choice = int(input("Enter your choice (1-7): "))
if choice == 1:
name = input("Enter event name: ")
importance = int(input("Enter event importance (1-10): "))
deadline_str = input("Enter event deadline (YYYY-MM-DD, leave blank if none): ")
deadline = datetime.datetime.strptime(deadline_str, "%Y-%m-%d") if deadline_str else None
notes.add_event(name, importance, deadline)
elif choice == 2:
notes.print_events()
index = int(input("Enter event index to delete: ")) - 1
notes.delete_event(index)
elif choice == 3:
notes.print_events()
index = int(input("Enter event index to modify: ")) - 1
name = input("Enter new event name (leave blank if no change): ")
importance = input("Enter new event importance (leave blank if no change): ")
deadline_str = input("Enter new event deadline (YYYY-MM-DD, leave blank if no change): ")
deadline = datetime.datetime.strptime(deadline_str, "%Y-%m-%d") if deadline_str else None
notes.modify_event(index, name, importance, deadline)
elif choice == 4:
notes.print_events()
elif choice == 5:
filename = input("Enter filename to save to: ")
notes.save_to_file(filename)
elif choice == 6:
filename = input("Enter filename to load from: ")
notes.load_from_file(filename)
elif choice == 7:
break
else:
print("Invalid choice, please try again.")
```
这个记事本可以让您输入事件名称、重要程度和截止日期,并按照重要程度排序。您可以使用菜单选项添加、删除、修改、打印、保存和加载事件。当您选择保存选项时,所有事件将保存到指定的文件中。当您选择加载选项时,程序将从指定的文件中加载事件。
阅读全文