我不熟悉Python OOP并尝试创建OOP程序来管理库。这段代码来自一本书。

此代码正常工作,但我需要了解action()在选择特定选项时如何调用相应函数,例如:当我选择1时,即使我们不调用show_notes函数,也会调用它。

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_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_notes(self, notes=None):

if not notes:

notes = self.notebook.notes

for note in notes:

print("{0}: {1}\n{2}".format(

note.id, note.tags, note.memo))

def search_notes(self):

filter = input("Search for: ")

notes = self.notebook.search(filter)

self.show_notes(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 = 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.py

import datetime

# Store the next available id for all new notes

last_id = 0

class Note:

'''Represent a note in the notebook. Match against a

string in searches and store tags for each note.'''

def __init__(self, memo, tags=''):

'''initialize a note with memo and optional

space-separated tags. Automatically set the note's

creation date and a unique id'''

self.memo = memo

self.tags = tags

self.creation_date = datetime.date.today()

global last_id

last_id += 1

self.id = last_id

def match(self, filter):

'''Determine if this note matches the filter

text. Return True if it matches, False otherwise.

Search is case sensitive and matches both text and

tags.'''

return filter in self.memo or filter in self.tags

class Notebook:

'''Represent a collection of notes that can be tagged,

modified, and searched.'''

def __init__(self):

'''Initialize a notebook with an empty list.'''

self.notes = []

def new_note(self, memo, tags=''):

'''Create a new note and add it to the list.'''

self.notes.append(Note(memo, tags))

def _find_note(self, note_id):

'''Locate the note with the given id.'''

for note in self.notes:

if str(note.id) == str(note_id):

return note

return None

def modify_memo(self, note_id, memo):

'''Find the note with the given id and change its

memo to the given value.'''

note = self._find_note(note_id)

if note:

note.memo = memo

return True

return False

def modify_tags(self, note_id, tags):

'''Find the note with the given id and change its

tags to the given value.'''

note = self._find_note(note_id)

if note:

note.tags = tags

return True

return False

def search(self, filter):

'''Find all notes that match the given filter

string.'''

return [note for note in self.notes if

note.match(filter)]

python oop求三角形面积公式_Python OOP --action()函数相关推荐

  1. python编程求三角形面积公式_python编程 输入三角形的三条边,计算三角形的面积\...

    展开全部 # -*- coding: UTF-8 -*- # Filename : test.py# author by : www.runoob.com a = float(input('输入三角6 ...

  2. python oop求三角形面积公式_Python面向对象编程-OOP

    Python面向对象编程-OOP 20141216 Chenxin整理 OOP的3大特性: 封装,继承,多态 一.封装 OOP目的,OOP为了代码重用 :分解代码 ,最小化代码的冗余以及对现在的代码进 ...

  3. python oop求三角形面积公式_python学习日记(OOP——类的内置方法)

    __str__和__repr__ 改变对象的字符串显示__str__,__repr__ 我们先定义一个Student类,打印一个实例: classStudent(object):def __init_ ...

  4. python海伦公式求三角形面积_python编程实战:海伦公式求取三角形的面积

    之前小编向大家介绍了在python中求取三角形面积的方法:三角形面积代码.大家对三角形面积的求取有了一定的了解,我们也知道计算机可以进行高精度的计算,那如果说在测量土地的面积的时候,不测三角形的高,只 ...

  5. python海伦公式求三角形面积程序流程图_《求三角形面积程序代码实现》教学设计...

    一.教学目标: 1 .能说出能否构成三角形的条件,并了解其 VB 表达式:能读懂求解三角形面积的程序流程图: 2 .了解编写程序代码的一般步骤,了解变量定义, IF 语句的基本语法格式: 3 .掌握 ...

  6. python求三角形面积步骤_python算三角形面积

    展开全部 代码如下: #!/usr/bin/python3 # -*- coding:utf-8 -*- """ @author:yaqon @file :shanjia ...

  7. c语言已知三个点坐标求三角形面积公式,c语言编程,求三角形面积公式?

    #include main() { float a,b,c,s,area; printf("请输入三个边长(以Enter键结束每次输入):\n"); scanf("%f% ...

  8. 2021-3-27春季个人赛补题(B - Minimal Area(叉乘法求三角形面积))

    B - Minimal Area(叉乘法求三角形面积) 题目链接: link. 原题描述: You are given a strictly convex polygon. Find the mini ...

  9. python求三角形面积

    运用Python求三角形面积,代码如下 在运行后,可得 输入三边长后通过三角形面积公式,可求得三角形的面积,其中需要得知三角形如何运用周长求面积,周长公式为s = (a + b + c) / 2,后用 ...

最新文章

  1. python语言教程-Python语言教程手册
  2. 开发商微信选房后不退认筹金_认筹金贸然转定金退不回 购房人认栽?
  3. 希尔伯特曲线的绘制c语言,用四叉树和希尔伯特曲线做空间索引
  4. 配合OAuth2进行单设备登录拦截
  5. python3 运行you get_一个基于Python3的神奇插件——you-get
  6. efuse 加密文件 linux,乐鑫关于“故障注入”和 “eFuse 保护”的安全建议 (CVE-2019-17391)...
  7. 客户购买产品的本质是什么,如何快速寻找到客户的需求,提高转化率?
  8. 匹配滤波器matlab实现_内插滤波器(Interpolated FIR)的FPGA实现
  9. bzoj1854 [Scoi2010]游戏
  10. 微软推出python免费在线教程视频_重磅发布!微软推出 Python 短视频入门课,直冲 GitHub 热榜第一!...
  11. 电脑f2还原系统步骤_电脑还原系统方法步骤详解
  12. 改写jtopo滚轮缩放代码
  13. 中文TTS文字转语言合成模块
  14. 机械设计基础课程设计【2】
  15. 中国视频制作服务市场现状研究分析与发展前景预测报告(2022)
  16. springboot2集成shiro认证鉴权(上篇)
  17. 【软件质量】-01-缺陷严重等级定义
  18. vue form表单验证清除
  19. 【测试】黑盒测试用例设计方法
  20. 2019该怎么学unity3D游戏开发?

热门文章

  1. 【codevs1036】商务旅行,LCA练习
  2. flash人物原地走路,Flash制作小人走路简单动画图文教程
  3. 游戏教玩家学java,技术|帮你学习Java语言的游戏
  4. python网站框架下载_最受欢迎的7款Python开源框架总结,忍不住收藏了~
  5. html页面上使用vlc,【JSJQuery】使用VLC在html中播放rtsp视频
  6. matlab createtask,Matlab批量与createjob
  7. Image-based Lighting approaches and parallax-corrected cubemap
  8. Adaline神经网络简单介绍和MATLAB简单实现
  9. 地平线:发布线NavNet众包高精地图采集与定位方案,牵手韩国最大通讯企业 | CES 2019...
  10. lvs-nat负载均衡实验