笔记本小程序代码源码
```python
class NoteBook:
def __init__(self):
self.notes = []
def add_note(self
note):
self.notes.append(note)
def view_notes(self):
if len(self.notes) == 0:
print("No notes available.")
else:
for i
note in enumerate(self.notes):
print(f"Note {i+1}: {note}")
def edit_note(self
index
new_note):
if index < 1 or index > len(self.notes):
print("Invalid note index.")
else:
self.notes[index-1] = new_note
print("Note edited successfully.")
def delete_note(self
index):
if index < 1 or index > len(self.notes):
print("Invalid note index.")
else:
del self.notes[index-1]
print("Note deleted successfully.")
# Sample code to demonstrate the NoteBook class
notebook = NoteBook()
print("Adding notes to the notebook...")
notebook.add_note("Note 1: This is the first note.")
notebook.add_note("Note 2: This is the second note.")
notebook.add_note("Note 3: This is the third note.")
print("Viewing notes in the notebook...")
notebook.view_notes()
print("Editing the second note...")
notebook.edit_note(2
"Note 2: This is the edited second note.")
print("Deleting the third note...")
notebook.delete_note(3)
print("Viewing notes in the notebook after editing and deleting...")
notebook.view_notes()
```
这段代码演示了一个简单的笔记本小程序的功能,包括添加、查看、编辑和删除笔记。通过NoteBook类,我们可以创建一个笔记本对象,并对其进行操作。在示例代码中,我们首先添加了三条笔记,然后查看了所有笔记。随后,我们编辑了第二条笔记并删除了第三条笔记。*,我们再次查看了笔记本中的所有笔记,确保编辑和删除操作已经生效。
这段代码可以作为一个基础的笔记本小程序的源码模板,您可以根据自己的需求进行修改和扩展。例如,您可以添加更多的功能,如搜索笔记、导出笔记、设置提醒等。希望这段代码能够对您有所帮助,祝您编程愉快!