python实现名片管理器的示例代码
更新时间:2019年12月17日 11:01:08 作者:易择365
这篇文章主要介绍了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
PyCharm安装PyQt5及其工具(Qt Designer、PyUIC、PyRcc)的步骤详解
这篇文章主要介绍了PyCharm安装PyQt5及其工具(Qt Designer、PyUIC、PyRcc)的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-11-11详解Python中__new__和__init__的区别与联系
在Python中,每个对象都有两个特殊的方法:__new__和__init__,本文将详细介绍这两个方法的不同之处以及它们之间的联系,具有一定的参考价值,感兴趣的可以了解一下2023-12-12
最新评论