python快速编程入门课本中的名片管理器_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实现名片管理器的示例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python怎么画两幅图_python
- 下一篇: python中的缩进问题_Python中
