Python object-oriented programming-an example of a Notepad program (ii)

Source: Internet
Author: User



Add a console menu to the program



menu.py


 
import sys from notebook import Notebook, Note class Menu: ‘‘‘Display a menu and respond to choices when run.‘‘‘ def __init__(self):
        self.notebook = Notebook()
        self.choices = { "1": self.show_all_notes, "2": self.search_notes, "3": self.add_note, "4": self.modify_note, "5": self.quit
        } def display_menu(self): print(""" Notebook Menu

1. Show all Notes
2. Search Notes
3. Add Note
4. Modify Note
5. Quit """) def run(self): ‘‘‘Display the menu and respond to choices.‘‘‘ while True:
            self.display_menu()
            choice = input("Enter an option: ")
            action = self.choices.get(choice) if action:
                action() else: print("{0} is not a valid choice".format(choice)) def show_all_notes(self, notes=None): if not notes:
            notes = self.notebook.notes for note in notes: print("{0}: {1}  {2}".format(
                note.id, note.tags, note.memo)) print("*********************************") def show_notes(self, notes=None): if not notes:
            notes = self.notebook.search_notes for note in notes: print("{0}: {1}  {2}".format(
                note.id, note.tags, note.memo)) print("*********************************") def search_notes(self):
        filter = input("Search for: ")
        search_notes = self.notebook.search(filter) #print(notes)  self.show_notes(search_notes) def add_note(self):
        memo = input("Enter a memo: ")
        self.notebook.new_note(memo) print("Your note has been added.") def modify_note(self):
        id = int(input("Enter a note id: "))
        memo = input("Enter a memo: ")
        tags = input("Enter tags: ") if memo:
            self.notebook.modify_memo(id, memo) if tags:
            self.notebook.modify_tags(id, tags) def quit(self): print("Thank you for using your notebook today.")
        sys.exit(0) if __name__ == "__main__":
    Menu().run()


Operation Result:



Notebook Menu



1. Show all Notes 2. Search Notes 3. ADD Note 4. Modify Note 5. Quit



Enter an Option:3 enter a memo:test Your note has been added. ... Enter an Option:3 enter a Memo:hello Your note has been added.



Enter an option:1 1:test ********************************* 2:hello ********************************* ... Enter an option:2 Search for:hel 2:hello ********************************* ... Enter an Option:4 Enter a note id:1 Enter a MEMO:AA enter Tags:1 <notebook. Note Object at 0x02b80fb0> ... Enter an option:1 1:1 AA ********************************* 2:hello ... Enter an Option:5 Thank your for using your notebook today.






Python object-oriented programming-an example of a Notepad program (ii)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.