给程序加上控制台菜单

menu.py

  1. import sys
  2. from notebook import Notebook, Note
  3.  
  4. class Menu:
  5. '''Display a menu and respond to choices when run.'''
  6. def __init__(self):
  7. self.notebook = Notebook()
  8. self.choices = {
  9. "": self.show_all_notes,
  10. "": self.search_notes,
  11. "": self.add_note,
  12. "": self.modify_note,
  13. "": self.quit
  14. }
  15.  
  16. def display_menu(self):
  17. print("""
  18. Notebook Menu
  19.  
  20. 1. Show all Notes
  21. 2. Search Notes
  22. 3. Add Note
  23. 4. Modify Note
  24. 5. Quit
  25. """)
  26.  
  27. def run(self):
  28. '''Display the menu and respond to choices.'''
  29. while True:
  30. self.display_menu()
  31. choice = input("Enter an option: ")
  32. action = self.choices.get(choice)
  33. if action:
  34. action()
  35. else:
  36. print("{0} is not a valid choice".format(choice))
  37.  
  38. def show_all_notes(self, notes=None):
  39. if not notes:
  40. notes = self.notebook.notes
  41. for note in notes:
  42. print("{0}: {1} {2}".format(
  43. note.id, note.tags, note.memo))
  44. print("*********************************")
  45.  
  46. def show_notes(self, notes=None):
  47. if not notes:
  48. notes = self.notebook.search_notes
  49. for note in notes:
  50. print("{0}: {1} {2}".format(
  51. note.id, note.tags, note.memo))
  52. print("*********************************")
  53.  
  54. def search_notes(self):
  55. filter = input("Search for: ")
  56. search_notes = self.notebook.search(filter)
  57. #print(notes)
  58. self.show_notes(search_notes)
  59.  
  60. def add_note(self):
  61. memo = input("Enter a memo: ")
  62. self.notebook.new_note(memo)
  63. print("Your note has been added.")
  64.  
  65. def modify_note(self):
  66. id = int(input("Enter a note id: "))
  67. memo = input("Enter a memo: ")
  68. tags = input("Enter tags: ")
  69. if memo:
  70. self.notebook.modify_memo(id, memo)
  71. if tags:
  72. self.notebook.modify_tags(id, tags)
  73.  
  74. def quit(self):
  75. print("Thank you for using your notebook today.")
  76. sys.exit(0)
  77.  
  78. if __name__ == "__main__":
  79. 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 <notebook.Note object at 0x02B80FB0> ... Enter an option: 1 1: 1  aa ********************************* 2:   hello ... Enter an option: 5 Thank you for using your notebook today.

Python面向对象编程 - 一个记事本程序范例(二)的更多相关文章

  1. Python面向对象编程 - 一个记事本程序范例(一)

    notebook.py import datetime last_id = 0 class Note: '''Represent a note in the notebook. Match again ...

  2. Python面向对象编程扑克牌发牌程序,另含大量Python代码!

    1. 题目 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不 ...

  3. python面向对象编程进阶

    python面向对象编程进阶 一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 ...

  4. Python 面向对象编程基础

    Python 面向对象编程基础 虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到. 一类: 语法: class 类名: 类属性,方法 ...

  5. Python面向对象编程——继承与派生

    Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ...

  6. python 面向对象编程(一)

    一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界中一些事物的封装,定义一个类可以采用下面的方式来定义: class c ...

  7. Python面向对象编程(下)

    本文主要通过几个实例介绍Python面向对象编程中的封装.继承.多态三大特性. 封装性 我们还是继续来看下上文中的例子,使用Student类创建一个对象,并修改对象的属性.代码如下: #-*- cod ...

  8. Python 面向对象编程 继承 和多态

    Python 面向对象编程 继承 和多态 一:多继承性 对于java我们熟悉的是一个类只能继承一个父类:但是对于C++ 一个子类可以有多个父亲,同样对于 Python一个类也可以有多个父亲 格式: c ...

  9. python面向对象编程学习

    python面向对象编程 基本概念理解 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作 ...

随机推荐

  1. Django1.7如何配置静态资源访问

    Django是非常轻量级的Web框架,今天散仙来看下如何在Django中配置静态的资源访问路径,一个中等规模的网站,可能就会有很多静态的资源需要访问,无论是html,txt,还是压缩包,有时候访问这些 ...

  2. Xilinx Platform Usb Cable

    Key Features High-performance FPGA configuration and PROM/CPLD programming Includes innovative FPGA- ...

  3. linux strace追踪mysql执行语句 (mysqld --debug)

    转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html http://blog.itpub.net/26250550/viewspace- ...

  4. ASP.NET WEB API 调试

    ASP.NET WEB API 中的路由调试与执行过程跟踪 http://www.cnblogs.com/Irving/p/4305493.html 路由调试 RouteDebugger 是调试 AS ...

  5. jQuery操作字符串

    var str = "我有一头小毛驴,我从来也不骑";   1.打印出某索引位置上的字符 //结果:毛 alert(str.charAt(5));   2.打印出某索引位置上的Un ...

  6. MVC文件上传05-使用客户端jQuery-File-Upload插件和服务端Backload组件自定义上传文件夹

    在零配置情况下,文件的上传文件夹是根目录下的Files文件夹,如何自定义文件的上传文件夹呢? MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小  MVC文 ...

  7. Major GC 是清理老年代。 Full GC 是清理整个堆空间—包括年轻代和老年代。

    Major GC 是清理老年代. Full GC 是清理整个堆空间—包括年轻代和老年代.

  8. CentOS6.5和RedHat6.5下以rpm方式安装mysql-5.6.20

    转帖;http://blog.csdn.net/mw08091020/article/details/39234207 a.检查下linux是不是已经安装了mysql rpm -qa | grep - ...

  9. ConcurrentBag扩展 批量加入

    public static void AddRange<T>(this ConcurrentBag<T> @this, IEnumerable<T> toAdd) ...

  10. Android平台上优秀的开源项目

    软件名:gaeproxy 软件作用:Android手机配置GoAgent. 项目地址:https://github.com/madeye/gaeproxy.git 软件名:ProxyDroid 软件作 ...