My Blog

My WordPress Blog

My Blog

My WordPress Blog

Note-Taking Application in Python

Create a file named note_taking_app.py and add the following code:

pythonCopy codeimport json
import os

class NoteTakingApp:
def __init__(self, filename='notes.json'):
    self.filename = filename
    self.notes = self.load_notes()
def load_notes(self):
    """Load notes from a JSON file."""
    if os.path.exists(self.filename):
        with open(self.filename, 'r') as file:
            return json.load(file)
    return []
def save_notes(self):
    """Save notes to a JSON file."""
    with open(self.filename, 'w') as file:
        json.dump(self.notes, file)
def add_note(self, note):
    """Add a new note."""
    self.notes.append(note)
    self.save_notes()
    print("Note added!")
def view_notes(self):
    """View all notes."""
    if not self.notes:
        print("No notes available.")
        return
    print("\nYour Notes:")
    for index, note in enumerate(self.notes, start=1):
        print(f"{index}. {note}")
    print()
def delete_note(self, index):
    """Delete a note by index."""
    if 0 <= index < len(self.notes):
        deleted_note = self.notes.pop(index)
        self.save_notes()
        print(f"Deleted note: {deleted_note}")
    else:
        print("Invalid note index.")
def run(self):
    """Run the note-taking application."""
    while True:
        print("Note-Taking Application")
        print("1. Add Note")
        print("2. View Notes")
        print("3. Delete Note")
        print("4. Exit")
        choice = input("Choose an option (1-4): ")
        if choice == '1':
            note = input("Enter your note: ")
            self.add_note(note)
        elif choice == '2':
            self.view_notes()
        elif choice == '3':
            self.view_notes()
            index = int(input("Enter the note number to delete: ")) - 1
            self.delete_note(index)
        elif choice == '4':
            print("Exiting the application.")
            break
        else:
            print("Invalid choice. Please try again.")
def main():
app = NoteTakingApp()
app.run()
if __name__ == "__main__":
main()

Step 2: Running the Note-Taking Application

  1. Open your terminal (or command prompt).
  2. Navigate to the directory where you saved note_taking_app.py.
  3. Run the script using the command:bashCopy codepython note_taking_app.py

How It Works

  • Data Storage: Notes are stored in a JSON file (notes.json). This allows for persistent storage of notes between sessions.
  • Functions:
    • load_notes: Loads existing notes from the JSON file.
    • save_notes: Saves the current notes back to the JSON file.
    • add_note: Adds a new note to the list.
    • view_notes: Displays all stored notes.
    • delete_note: Deletes a note by its index.
  • User Interface: The command-line interface allows users to choose actions like adding, viewing, or deleting notes.
Note-Taking Application in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top