Python之名片管理函数版
一、源码
info_storage=[] def print_menu(): print("="*50) print("The card manager program v 1.0") print("1.add a new card") print("2.delete a card") print("3.modify a card") print("4.select check a card") print("5.show all") print("6.exit") print("="*50) def add_new_card_infor(): global info_storage new_name=input("please input your name:") new_qq=int(input("please input your qq:")) new_weixin=input("please input your weixin:") new_addr=input("please input your addr:") new_info={} new_info["name"]=new_name new_info["qq"]=new_qq new_info["weixin"]=new_weixin new_info["addr"]=new_addr info_storage.append(new_info) def find_card_infor(): global info_storage find_name=input("please input your want to find the name:") find_flag=0 for card in info_storage: if card['name']==find_name: if find_flag==0: print("name\tqq\tweixin\taddr\t") print("%s\t%d\t%s\t%s\t"%(card['name'],card['qq'],card['weixin'],card['addr'])) find_flag=1 else: continue if find_flag==0: print("can't find it") def show_all_infor(): print("name\tqq\tweixin\taddr\t") for card in info_storage: print("%s\t%d\t%s\t%s\t"%(card['name'],card['qq'],card['weixin'],card['addr'])) #1.usage def main(): """control panel""" #2.catch user input print_menu() while True: num = int(input("please input select nmuber:")) #3.accroding to user data to start function if num==1: add_new_card_infor() elif num==2: name=input("please input your name:") elif num==3: pass elif num==4: find_card_infor() elif num==5: show_all_infor() elif num==6: break else: print("error") print("="*50) main()
二、完善
1.函数的作用就是使代码分块,当然面向对象开发的基础,在上述代码中应该加上注释,这样在使用Help获取帮助的时候,才会有完善的信息。
e.g.:
def main():
"""control panel"""#使用三个双引号、三个单引号都可以
2.删除card并没有写,删除也是查找的过程,这样思考的话,我们应该调用查找的函数,利用该函数获取到列表的下标,进行删除。
使用方法:
del xxx[下标] 根据下标来删除
我在修改过程中出现了一个问题,先说明一下思路:
1.删除也需要对列表进行查找
2.因此我利用了查找的方法,适当地修改将查询到的下标进行保存
3.那么问题就出现了使用del xx[下标]的方式删除info_storage列表中的数据,会导致下标发生改变,就是说删除了一个数据,这个数据后面的下标将自动减1,所以保存的下标就没有用了,会乱删除数据的
4.我就采用了用"del"字符串去填充info_storage列表 e.g.
假如: info_storage=[1,2,3,4]
我查找的要删除的下标为sub=[0,3],那么就是要删除info_storage中的1和4,得到结果info_storage=[2,3]
那么使用"del"填充以后 info_storage[sub[0]]="del",使用循环进行替换后得到 info_storage=["del",2,3,"del"]
4.用得到的新列表,查找"del"进行删除,使用info_storage.remove("del")
完善后的源代码:
info_storage=[] sub=[] def print_menu(): print("="*50) print("The card manager program v 1.0") print("1.add a new card") print("2.delete a card") print("3.modify a card") print("4.select check a card") print("5.show all") print("6.exit") print("="*50) def add_new_card_infor(): global info_storage new_name=input("please input your name:") new_qq=int(input("please input your qq:")) new_weixin=input("please input your weixin:") new_addr=input("please input your addr:") new_info={} new_info["name"]=new_name new_info["qq"]=new_qq new_info["weixin"]=new_weixin new_info["addr"]=new_addr info_storage.append(new_info) def delete_card_infor(): find_card_infor() if len(sub)>0: for temp in sub: info_storage[temp]="del" for temp in info_storage: if temp=="del": info_storage.remove(temp) else: continue else: print("please input a card,usage 1") def find_card_infor(): global info_storage find_name=input("please input your want to find the name:") find_flag=0 temp=0 sub1=[] if len(info_storage)>0: for card in info_storage: temp+=1 if card['name']==find_name: sub1.append(temp-1) if find_flag==0: print("name\tqq\tweixin\taddr\t") print("%s\t%d\t%s\t%s\t"%(card['name'],card['qq'],card['weixin'],card['addr'])) find_flag=1 else: continue if find_flag==0: print("can't find it") else: print("please input a card,usage 1") global sub sub=sub1 def show_all_infor(): if len(info_storage)>0: print("name\tqq\tweixin\taddr\t") for card in info_storage: print("%s\t%d\t%s\t%s\t"%(card['name'],card['qq'],card['weixin'],card['addr'])) else: print("empty") #1.usage def main(): """control panel""" #2.catch user input print_menu() while True: num = int(input("please input select nmuber:")) #3.accroding to user data to start function if num==1: add_new_card_infor() elif num==2: delete_card_infor() elif num==3: find_card_infor() elif num==4: find_card_infor() elif num==5: show_all_infor() elif num==6: break else: print("error") print("="*50) main()
三、总结
总得来说,有点曲线救国的意思,当然我们也可以不用重复利用find_card_info这个函数来进行删除,写单独的删除方法,边查边删除。这样也是一种方式,并且还简单一些。
四、作业:
写单独的删除方法

浙公网安备 33010602011771号