|
| 1 | +# **Last Project: Simple Notepad Application with Classes** |
| 2 | + |
| 3 | +## **Project Description:** |
| 4 | +Create a Simple Notepad Application in Python using classes. This project will help you improve your file handling, user input, and basic data management skills. |
| 5 | + |
| 6 | +### **Key Features:** |
| 7 | +1. Create a user interface that prompts the user to enter a note title and content. |
| 8 | +2. Save user-entered notes to a text file. |
| 9 | +3. Add a "View Notes" option so that the user can list the saved notes. |
| 10 | +4. Add options that allow the user to delete or edit a specific note. |
| 11 | + |
| 12 | +### **Extra Tips:** |
| 13 | +- Allow the user to provide a unique ID for each note, so they can manage notes more easily. |
| 14 | +- Get confirmation from the user when editing or deleting notes and add security checks against errors. |
| 15 | +- You can add a custom display layout to display your notes by formatting or coloring them properly. |
| 16 | + |
| 17 | +**Project Implementation:** |
| 18 | + |
| 19 | +```python |
| 20 | +import os |
| 21 | +import os |
| 22 | + |
| 23 | +class Notepad: |
| 24 | + def __init__(self): |
| 25 | + self.notes = [] |
| 26 | + |
| 27 | + def create_note(self): |
| 28 | + note_id = input("Enter a unique ID for the note: ") |
| 29 | + title = input("Enter the note title: ") |
| 30 | + content = input("Enter the note content: ") |
| 31 | + self.notes.append({"id": note_id, "title": title, "content": content}) |
| 32 | + print("Note has been created.") |
| 33 | + |
| 34 | + def view_notes(self): |
| 35 | + if not self.notes: |
| 36 | + print("No notes found.") |
| 37 | + else: |
| 38 | + print("Available Notes:") |
| 39 | + for note in self.notes: |
| 40 | + print(f"ID: {note['id']}, Title: {note['title']}") |
| 41 | + |
| 42 | + def edit_note(self): |
| 43 | + note_id = input("Enter the ID of the note you want to edit: ") |
| 44 | + for note in self.notes: |
| 45 | + if note['id'] == note_id: |
| 46 | + new_title = input(f"Enter a new title for note {note_id}: ") |
| 47 | + new_content = input(f"Enter new content for note {note_id}: ") |
| 48 | + note['title'] = new_title |
| 49 | + note['content'] = new_content |
| 50 | + print(f"Note {note_id} has been edited.") |
| 51 | + return |
| 52 | + print(f"No note found with ID {note_id}.") |
| 53 | + |
| 54 | + def delete_note(self): |
| 55 | + note_id = input("Enter the ID of the note you want to delete: ") |
| 56 | + for note in self.notes: |
| 57 | + if note['id'] == note_id: |
| 58 | + self.notes.remove(note) |
| 59 | + print(f"Note {note_id} has been deleted.") |
| 60 | + return |
| 61 | + print(f"No note found with ID {note_id}.") |
| 62 | + |
| 63 | + def save_notes_to_file(self): |
| 64 | + with open("notes.txt", "w") as file: |
| 65 | + for note in self.notes: |
| 66 | + file.write(f"ID: {note['id']}\n") |
| 67 | + file.write(f"Title: {note['title']}\n") |
| 68 | + file.write(f"Content: {note['content']}\n") |
| 69 | + file.write("\n") |
| 70 | + |
| 71 | + def load_notes_from_file(self): |
| 72 | + if os.path.isfile("notes.txt"): |
| 73 | + with open("notes.txt", "r") as file: |
| 74 | + lines = file.readlines() |
| 75 | + note_info = {} |
| 76 | + for line in lines: |
| 77 | + if line.strip() == "": |
| 78 | + if note_info: |
| 79 | + self.notes.append(note_info) |
| 80 | + note_info = {} |
| 81 | + else: |
| 82 | + key, value = line.strip().split(": ") |
| 83 | + note_info[key.lower()] = value |
| 84 | + if note_info: |
| 85 | + self.notes.append(note_info) |
| 86 | + else: |
| 87 | + print("No notes file found.") |
| 88 | + |
| 89 | + def run(self): |
| 90 | + self.load_notes_from_file() |
| 91 | + while True: |
| 92 | + print("\nSimple Notepad Application Menu:") |
| 93 | + print("1. Create a Note") |
| 94 | + print("2. View Notes") |
| 95 | + print("3. Edit a Note") |
| 96 | + print("4. Delete a Note") |
| 97 | + print("5. Save Notes to File") |
| 98 | + print("6. Exit") |
| 99 | + choice = input("Enter your choice: ") |
| 100 | + |
| 101 | + if choice == '1': |
| 102 | + self.create_note() |
| 103 | + elif choice == '2': |
| 104 | + self.view_notes() |
| 105 | + elif choice == '3': |
| 106 | + self.edit_note() |
| 107 | + elif choice == '4': |
| 108 | + self.delete_note() |
| 109 | + elif choice == '5': |
| 110 | + self.save_notes_to_file() |
| 111 | + elif choice == '6': |
| 112 | + print("Exiting Notepad Application.") |
| 113 | + break |
| 114 | + else: |
| 115 | + print("Invalid choice. Please try again.") |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + notepad = Notepad() |
| 119 | + notepad.run() |
| 120 | +``` |
| 121 | + |
| 122 | +**Project Outcome:** |
| 123 | +By completing this project, you will have a functional Notepad Application that allows users to create, view, edit, and delete notes, as well as save and load notes from a file. This project will enhance your Python skills and provide practical experience in building text-based applications. |
| 124 | + |
| 125 | +Feel free to customize and expand this project as you see fit, adding more features or improving the user interface to make it even more user-friendly. Enjoy coding! |
0 commit comments