当前位置:首页 » python教程 » 正文

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

看: 975次  时间:2021-01-08  分类 : 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博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!