python实现名片管理器的示例代码

yipeiwu_com7年前Python基础

编写程序,完成“名片管理器”项目

需要完成的基本功能:

  • 添加名片
  • 删除名片
  • 修改名片
  • 查询名片
  • 退出系统

程序运行后,除非选择退出系统,否则重复执行功能

mingp.py

# 名片类:(参数)
# # 添加名片功能
# # 删除名片功能:
# # 修改名片功能:
# # 查询名片功能:
class MingPian():
  def __init__(self,all_dict,name,age):
    self.all_dict=all_dict
    self.name=name
    self.age=age
 
  def tianjia(self):
    my_dict = {"name": self.name, "age": self.age}
    self.all_dict[self.name]=my_dict
    print("添加名片成功....")
    return self.all_dict
    # print(self.all_dict) #测试添加函数可否正常执行
 
  def shanchu(self):
    if self.name in self.all_dict:
      del self.all_dict[self.name]
      print("删除成功")
    else:
      print("输入名字有误")
    return self.all_dict
 
  def xiugai(self):
    if self.name in self.all_dict:
      self.age = input("请输入修改后的年龄:")
      self.all_dict[self.name]["age"] = self.age
      print("修改成功")
    else:
      print("输入名字有误")
    return self.all_dict
 
  def chaxun(self):
    if self.name in self.all_dict:
      n = self.all_dict[self.name]["name"]
      a = self.all_dict[self.name]["age"]
      print("姓名:%s 年龄:%s" % (n, a))
    else:
      print("输入名字有误")
 
#test
# all_dict = {}
# MingPian(all_dict,'xiaoming','20').tianjia()

base.py

# 选择判断函数:
from mingpian.mingp import MingPian
 
class Base(MingPian):
  def __init__(self,all_dict,name,age,index):
    #为了能使用或扩展父类的行为,最好显示调用父类的__init__方法
    # 子类调用父类的构造函数进行初始化
    # 通过子类把参数传给父类(self不能少,self只有在实例化和实例调用类时才能省略,此处不是)
    #super(Base,self).__init__(all_dict,name,age)
    MingPian.__init__(self,all_dict,name,age)
    self.index=index
 
  #初始化
  def caozuo(self):
    if self.index == "1":
      self.name = input("请输入您的名字:")
      self.age = input("请输入您的年龄:")
      # 子类调用父类方法
      # 子类在调用父类方法必须要传self
      MingPian.tianjia(self)
 
    elif self.index == "2":
      self.name = input("请输入要删除数据的名字:")
      MingPian.shanchu(self)
 
    elif self.index == "3":
      self.name = input("请输入要修改信息人的名字:")
 
      MingPian.xiugai(self)
 
    elif self.index == "4":
      self.name = input("请输入您要查询的名字:")
      MingPian.chaxun(self)
 
    elif self.index == "5":
      print("欢迎下次使用,再见!")
      exit()

main.py

# where True:
# 展示菜单函数
# 选择判断函数()
#  判断选择的操作菜单
from mingpian.base import Base
 
all_dict = {}
info_str = """1.添加名片
2.删除名片
3.修改名片
4.查询名片
5.退出系统
请选择:"""
 
while True:
  index = input(info_str)
  kaishi=Base(all_dict,0,0,index)
  kaishi.caozuo()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python multiprocessing.Manager介绍和实例(进程间共享数据)

Python中进程间共享数据,处理基本的queue,pipe和value+array外,还提供了更高层次的封装。使用multiprocessing.Manager可以简单地使用这些高级接...

Python编程中的反模式实例分析

本文实例讲述了Python编程中的反模式。分享给大家供大家参考。具体分析如下: Python是时下最热门的编程语言之一了。简洁而富有表达力的语法,两三行代码往往就能解决十来行C代码才能解...

python运行时间的几种方法

最早见过手写的,类似于下面这种: import datetime def time_1(): begin = datetime.datetime.now() sum =...

详解Python打包分发工具setuptools

详解Python打包分发工具setuptools

Python打包分发工具setuptools:曾经 Python 的分发工具是 distutils,但它无法定义包之间的依赖关系。setuptools 则是它的增强版,能帮助我们更好的创...

python实现的多线程端口扫描功能示例

python实现的多线程端口扫描功能示例

本文实例讲述了python实现的多线程端口扫描功能。分享给大家供大家参考,具体如下: 下面的程序给出了对给定的ip主机进行多线程扫描的Python代码 #!/usr/bin/env...