Python面向对象编程 - 一个记事本程序范例(二)
给程序加上控制台菜单
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 = {
"": self.show_all_notes,
"": self.search_notes,
"": self.add_note,
"": self.modify_note,
"": 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()
运行结果:
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面向对象编程 - 一个记事本程序范例(二)的更多相关文章
- Python面向对象编程 - 一个记事本程序范例(一)
notebook.py import datetime last_id = 0 class Note: '''Represent a note in the notebook. Match again ...
- Python面向对象编程扑克牌发牌程序,另含大量Python代码!
1. 题目 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不 ...
- python面向对象编程进阶
python面向对象编程进阶 一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 ...
- Python 面向对象编程基础
Python 面向对象编程基础 虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到. 一类: 语法: class 类名: 类属性,方法 ...
- Python面向对象编程——继承与派生
Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ...
- python 面向对象编程(一)
一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界中一些事物的封装,定义一个类可以采用下面的方式来定义: class c ...
- Python面向对象编程(下)
本文主要通过几个实例介绍Python面向对象编程中的封装.继承.多态三大特性. 封装性 我们还是继续来看下上文中的例子,使用Student类创建一个对象,并修改对象的属性.代码如下: #-*- cod ...
- Python 面向对象编程 继承 和多态
Python 面向对象编程 继承 和多态 一:多继承性 对于java我们熟悉的是一个类只能继承一个父类:但是对于C++ 一个子类可以有多个父亲,同样对于 Python一个类也可以有多个父亲 格式: c ...
- python面向对象编程学习
python面向对象编程 基本概念理解 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作 ...
随机推荐
- poj 3164
朱刘算法 步骤: 1.计算出每个点边权最小的边的权(如果除根以外有其他的点没有入边,则不存在最小树形图),并记下边的另一个端点(称其为这个点的前趋) 2.沿着每个点向上走,如果在走到根或环上的点之前, ...
- Linux 内核编译步骤及配置详解
前言 Linux内核是操作系统的核心,也是操作系统最基本的部分. Linux内核的体积结构是单内核的.但是他充分采用了微内核的设计思想.使得虽然是单内核.但工作在模块化的方式下.并且这个模块可以 ...
- How far away ?(DFS)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- ICD2 VPP limiter for new PIC microcontrollers.
http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...
- hybrid App h5二级页面返回的时候保持与一级页面浏览的位置一致
最近在开发公司hybrid app的时候,需要将原本原生的配置中心模块统一变更为H5,做完之后从测试那里反馈回来这样一个问题,当滑到页面底部或中部的时候进入子页面进行设置,返回的时候页面应该定位到离开 ...
- mybatis逆向工程--自动生成实体代码(mybatis-generator)
随便找个目录, 添加文件, 如图 主要是两个jar包, generator的下载路径: https://github.com/mybatis/generator/releases 驱动包随 ...
- linux普通用户获取管理员权限
原文:http://www.cnblogs.com/likwo/p/3435404.html 测试环境:CentOS 5.5 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #ad ...
- python接口自动化26-参数关联和JSESSIONID(上个接口返回数据作为下个接口请求参数)
前言 参数关联是接口测试和性能测试最为重要的一个步骤,很多接口的请求参数是动态的,并且需要从上一个接口的返回值里面取出来,一般只能用一次就失效了. 最常见的案例就是网站的登录案例,很多网站的登录并不仅 ...
- etcd集群搭建
etcd介绍,以及适用场景,参考:http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement ...
- Windows Server 2003 IIS设置完全篇
一.启用Asp支持Windows Server 2003 默认安装,是不安装 IIS 6 的,需要另外安装.安装完 IIS 6,还需要单独开启对于 ASP 的支持. 第一步,启用Asp,进入:控制面板 ...