List or dictionary make global variable call in function can not add global
#used to store business cardsCard_infors = []defPrint_menu ():"""Finish the Print function menu""" Print('='* 50) Print('Business Card Management system function Edition') Print('1. Add a new business card') Print('2. Delete a business card') Print('3. Modify a business card') Print('4. Query a business card') Print('5. Show All business cards') Print('6. Launch the system') Print('='* 50)defadd_new_card_infor ():"""complete the Add a new business card"""new_name= Input ('Please enter a new name:') New_qq= Input ('Please enter the new QQ:') New_weixin= Input ('Please enter a NEW:') new_addr= Input ('Please enter a new address:') #define a new dictionary to store a new business cardNew_infor ={} new_infor['name'] =new_name new_infor['QQ'] =new_qq new_infor['Weixin'] =new_weixin new_infor['Addr'] =new_addr#add a dictionary to the list Globalcard_infors card_infors.append (new_infor)deffind_card_infor ():"""query a business card""" Globalcard_infors Find_name= Input ('Please enter the name you want to query:') Find_flag=0 forTempinchcard_infors:ifFind_name = = temp['name']: Print("%s\t%s\t%s\t%s\t"% (temp['name'], temp['QQ'], temp['Weixin'], temp['Addr'])) Find_flag= 1 Break ifFind_flag = =0:Print("no such person ....")defshow_all_infor ():"""Show All business card information""" Globalcard_inforsPrint("name \tqq\t\t address") forTempinchcard_infors:Print("%s\t%s\t%s\t%s"% (temp['name'], temp['QQ'], temp['Weixin'], temp['Addr']))defMain ():#1. Print function TipsPrint_menu () whileTrue:num= Int (Input ('get the user's input number:')) ifnum = = 1: Add_new_card_infor ()elifnum = = 2: Pass elifnum = = 3: Pass elifnum = = 4: Find_card_infor ()elifnum = = 5: Show_all_infor ()elifnum = = 6: Break Else: Print('input Error, please re-enter') Print(" ")#calling the main functionMain ()
Python practiced hand Series--Business card management system