The examples in this article share the specific code of the Python version of the business card management system for your reference. The specific content is as follows
First create the cards_main file
import cards_tools
# Infinite loop, the user decides when to exit
while True:
# TODO comments, used to mark the work that needs to be done
cards_tools.show_menu()
action_str =raw_input("Please select the operation you want to perform: ")print("The operation you choose is%s"% action_str)
#1,2,3 Operations for business cards
if action_str in["1","2","3"]:if action_str =="1":
cards_tools.new_card()
elif action_str =="2":
cards_tools.show_all()
elif action_str =="3":
cards_tools.search_card()
#0 Exit system
elif action_str =="0":print("Welcome to use [Business Card Management System] again:")break
# If you do not want to write the code inside the branch immediately when you are developing the program
# You can use the pass keyword to represent a placeholder to ensure that the code structure of the program is correct
# When running the program, the pass keyword does nothing
else:print("Input errors, please re-enter:")
Create another cards_tools file
card_list =[]
def show_menu():"""Show menu"""
print '*'*50
print 'Welcome to [Business Card Management System]'
print ''
print '1.Add business card'
print '2.display all'
print '3.Search for business cards'
print '0.Exit system'
print '*'*50
def new_card():"""Add business card"""
print '-'*50
print 'Add business card'
# 1. Prompt the user to enter the details of the business card
name_str =raw_input('Please type in your name:')
phone_str =raw_input('Please enter the phone:')
qq_str =raw_input('Please enter QQ:')
email_str =raw_input('please input your email:')
# 2. Use the information entered by the user to build a business card dictionary
card_dict ={'name_str': name_str,'phone_str': phone_str,'qq_str': qq_str,'email_str': email_str}
# 3. Add business card dictionary to the list
card_list.append(card_dict) #Append a dictionary to a list
print card_list
# 4. Prompt the user to add successfully
print 'Add to%s business card success'% name_str
def show_all():"""Show all business cards"""
print '-'*50
print 'Show all business cards'
# Determine whether there is a business card record, if not, prompt the user and return
iflen(card_list)==0:
print 'There is currently no business card record, please use the new function to add a business card'
# return can return the execution result of a function
# The code below will not be executed
# If there is nothing after return, it means that it will return to the place where the function was called
# And returns no results
return
# Print head
for name in["Name","phone","QQ","mailbox"]:
print name,
print ''
# Print divider
print '='*50
# Traverse the list of business cards and output dictionary information in turn
for card_dict in card_list:
# print card_dict
print '%stt%stt%stt%s'%(card_dict['name_str'],
card_dict['phone_str'],
card_dict['qq_str'],
card_dict['email_str'])
def search_card():"""Search for business cards"""
print '-'*50
print 'Search for business cards'
# 1. Prompt the user to enter the name to search
find_name =raw_input('Please enter the name to be searched:')
# 2. Traverse the list of business cards, query the name to be searched, if not found, you need to prompt the user
for card_dict in card_list:if card_dict['name_str']== find_name:
print 'Name Phone QQ Email'
print '='*50
print '%s %s %s %s'%(card_dict['name_str'],
card_dict['phone_str'],
card_dict['qq_str'],
card_dict['email_str'])
# TODO performs modification and deletion operations on the business card records found
# In our daily writing programs, if there is too much code for a function, it is difficult to read and write. In development, a function can be encapsulated for a specific independent function, and this function will handle the specific Operation, so that we can ensure that the code in each function is clear and clear, and the function is clear
deal_card(card_dict)breakelse:
print 'Sorry, not found%s'% find_name
def deal_card(find_dict):
print find_dict
action_str =raw_input('Please select the action to be performed''[1]modify[2]delete:')
# Replace existing key-value pairs
if action_str =='1':
find_dict['name_str']=input_card_info(find_dict['name_str'],'Name:')
find_dict['phone_str']=input_card_info(find_dict['phone_str'],'phone:')
find_dict['qq_str']=input_card_info(find_dict['qq_str'],'QQ:')
find_dict['email_str']=input_card_info(find_dict['email_str'],'mailbox:')
print 'Successfully modified the business card! ! !'
elif action_str =='2':
card_list.remove(find_dict)
print 'Successfully deleted the business card! ! !'
def input_card_info(dict_value, tip_message):"""
: param dict_value:The original value in the dictionary
: param tip_message:Prompt text
: return:If the user enters the content, it returns the content and is responsible for returning the original value in the dictionary
"""
# 1. Prompt the user for input
result_str =raw_input(tip_message)
# 2. Judge the user's input, if the user enters the content, return the result directly
iflen(result_str)0:return result_str
# 3. If the user does not input content, return the original value in the dictionary'
else:return dict_value
Effect picture:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support website (zalou.cn).
Recommended Posts