python记事本编程_Python面向对象编程 - 一个记事本程序范例(二)

给程序加上控制台菜单

menu.py

importsysfrom notebook importNotebook, NoteclassMenu:'''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

}defdisplay_menu(self):print("""Notebook Menu

1. Show all Notes

2. Search Notes

3. Add Note

4. Modify Note

5. Quit""")defrun(self):'''Display the menu and respond to choices.'''

whileTrue:

self.display_menu()

choice= input("Enter an option:")

action=self.choices.get(choice)ifaction:

action()else:print("{0} is not a valid choice".format(choice))def show_all_notes(self, notes=None):if notnotes:

notes=self.notebook.notesfor note innotes:print("{0}: {1} {2}".format(

note.id, note.tags, note.memo))print("*********************************")def show_notes(self, notes=None):if notnotes:

notes=self.notebook.search_notesfor note innotes:print("{0}: {1} {2}".format(

note.id, note.tags, note.memo))print("*********************************")defsearch_notes(self):

filter= input("Search for:")

search_notes=self.notebook.search(filter)#print(notes)

self.show_notes(search_notes)defadd_note(self):

memo= input("Enter a memo:")

self.notebook.new_note(memo)print("Your note has been added.")defmodify_note(self):

id= int(input("Enter a note id:"))

memo= input("Enter a memo:")

tags= input("Enter tags:")ifmemo:

self.notebook.modify_memo(id, memo)iftags:

self.notebook.modify_tags(id, tags)defquit(self):print("Thank you for using your notebook today.")

sys.exit(0)if __name__ == "__main__":

Menu().run()

运行结果:

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 ... Enter an option: 1 1: 1 aa ********************************* 2: hello ... Enter an option: 5 Thank you for using your notebook today.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值