Comprehensive application of Python foundation -- Development

Why_ does_ it_ work 2021-11-25 15:24:00

Comprehensive review

  • Variable
  • loop
  • function
  • modular

Development   Business card management system

Introduction to business card management system

Business card management system can be understood as roster Software , After creating a new person's information through an individual, you can query and other simple procedures

Business card management system has three functions ,

1. New business card

2. Show all business cards

3. Check the business card

Welcome to use 【 Business card management system 】V1.0

1. New business card

2. Show all

3. Check the business card

0. Exit the system

Business card management system development

1. Construction of framework

2. New business card

3. Show all business cards

4. Check the business card

5. Modify and delete the business card after successful query

6. Give Way python The program runs directly


1. Construction of framework

What we need to do when developing programs is to build frameworks

The first thing you need to do to build a framework is Ready to file ( What the file needs is the main file and tool file ), You also need to prepare some basic code that can run easily

about Ready to file , The main file is main file , All operations must be started here

about Tool files , Is to perform other operations on this program ( Delete , Add )

First create One main file

Infinite loop

  • When developing software , If   Don't want the program to run after   Quit immediately

  • You can add a   Infinite loop

  • It's up to the user to decide   Time to exit the program

When the user enters a number to program , That is, the correct operation , The for correct (true) After that, the main file performs subsequent operations

​
while True:
​
# TODO( Xiao Ming ) Display system menu
​
action = input(" Please select operation function :")
​
print(" The operation you choose is :%s" % action)
​
# Determine the subsequent operations according to the user's input
if action in ["1", "2", "3"]:
pass
elif action == "0":
print(" Welcome to use again 【 Business card management system 】")
​
break
else:
print(" Input error , Please re-enter ")
​

What we need to pay attention to here is

  1. Use  in  in the light of   list   Judge , Avoid using  or  Splicing complex logical conditions

  2. Not used  int  Convert user input , You can avoid   Once the user enters

  • pass  It's just an empty statement , Not doing anything , Generally used as occupation statement

  • To maintain the integrity of the program structure

about Tool files

  In the business card management system , The purpose of the tool file is

1. Show menu

2. New business card

3. Show all

4. Search for business cards

So for tool files , The way we take is , Create four functions

def show_menu():
​
""" Show menu """
pass
​
def new_card():
​
""" New business card """
print("-" * 50)
print(" function : New business card ")
​
​
def show_all():
​
""" Show all """
print("-" * 50)
print(" function : Show all ")
​
​
def search_card():
​
""" Search for business cards """
print("-" * 50)
print(" function : Search for business cards ")
​

Be careful : The function of three quotation marks is to output one by one  

After completing the master file and tool file, we should import the four functions of the tool file for optimization

use import

import cards_tools
​
while True:
​
cards_tools.show_menu()
​
action = input(" Please select operation function :")
​
print(" The operation you choose is :%s" % action)
​
# Determine the subsequent operations according to the user's input
if action in ["1", "2", "3"]:
​
if action == "1":
cards_tools.new_card()
​
elif action == "2":
cards_tools.show_all()
​
elif action == "3":
cards_tools.search_card()
​
elif action == "0":
print(" Welcome to use again 【 Business card management system 】")
​
break
else:
print(" Input error , Please re-enter :")
​

So far, the operation of our business card management system is roughly It's done  

The next step is to improve the function operation of each tool file

2. New business card

The previous article said , Programs are used to run data , Variables store data

Then our users need to create new business cards, so , Need to save information to the dictionary , Add a business card dictionary to the list , At this time, our users will be added successfully

Define business card list variables

  • stay  cards_tools  Add a... To the top of the file   List variables

  • # A list of all business card records
    card_list = []
    def new_card():
    ​
    """ New business card
    """
    print("-" * 50)
    print(" function : New business card ")
    ​
    # 1. Prompt the user to input the business card information
    name = input(" Please enter a name :")
    phone = input(" Please input the phone number :")
    qq = input(" Please enter QQ number :")
    email = input(" Please enter email address :")
    ​
    # 2. Save user information to a dictionary
    card_dict = {"name": name,
    "phone": phone,
    "qq": qq,
    "email": email}
    ​
    # 3. Add user dictionary to business card list
    card_list.append(card_dict)
    ​
    print(card_list)
    # 4. Prompt to add success message
    print(" Successful addition %s Business card of " % card_dict["name"])
    ​

3. Show all business cards

If you want to display all business cards , Now the business card dictionary is all on the list , So we can output all the lists directly Be careful : For better display , We should make the dividing line of the header to facilitate users to watch , Then there is the following code

def show_all():
""" Show all
"""
print("-" * 50)
print(" function : Show all ")
​
# Print header
for name in [" full name ", " Telephone ", "QQ", " mailbox "]:
print(name, end="\t\t")
​
print("")
​
# Print separator lines
print("=" * 50)
​
for card_dict in card_list:
​
print("%s\t\t%s\t\t%s\t\t%s" % (card_dict["name"],
card_dict["phone"],
card_dict["qq"],
card_dict["email"]))

Pay attention to is

print("\tPython")#\t Represents four empty characters , Also called indent , It's equivalent to clicking Tab key 
print("\nPython\nJAVA\nC++\nJavaScript\nC#")#\n Means line break , It's equivalent to pressing enter 
print("languages:\n\tPython\n\tJAVA\n\tC++\n\tJavaScript\n\tC#")#\n\t Means line feed plus four spaces in each line 

  If there is no business card, the dictionary is saved in the list , Then there is the following code , This code is placed above the code above

def show_all():
""" Show all
"""
print("-" * 50)
print(" function : Show all ")
​
# 1. Judge if there is a business card record
if len(card_list) == 0:
print(" Tips : There are no business card records ")
​
return
​

return You can return the result of a function

The following code will not be executed , This avoids subsequent operations , Users can directly understand , No record of business card code , Prompt the user and return

4. Check the business card

  1. Prompt the user for the name to search

  2. Traverse the list according to the name entered by the user

  3. Search for the specified business card , And then follow up

def search_card():
​
""" Search for business cards
"""
print("-" * 50)
print(" function : Search for business cards ")
​
# 1. Prompt for names to search
find_name = input(" Please enter the name you want to search :")
​
# 2. Ergodic dictionary
for card_dict in card_list:
​
if card_dict["name"] == find_name:
​
print(" full name \t\t\t Telephone \t\t\tQQ\t\t\t mailbox ")
print("-" * 40)
print("%s\t\t\t%s\t\t\t%s\t\t\t%s" % (
card_dict["name"],
card_dict["phone"],
card_dict["qq"],
card_dict["email"]))
​
print("-" * 40)
# TODO( Xiao Ming ) Follow up on the dictionary found : modify / Delete
​
break
else:
print(" Can't find %s" % find_name)
​

for After traversal, it does not pass break come out execute else Branch  


Add card operation function : modify / Delete / Back to main menu

def deal_card(find_dict):
​
""" Operate the business card dictionary found
​
:param find_dict: Found the business card Dictionary
"""
print(find_dict)
​
action_str = input(" Please select the operation to be performed "
"[1] modify [2] Delete [0] Go back to the upper menu ")
​
if action == "1":
print(" modify ")
elif action == "2":
print(" Delete ")
​

5. Modify and delete the business card after successful query

Delete the dictionary in the list

  • Because the dictionary record found has been saved in the list

  • To delete a business card record , Just delete the corresponding dictionary in the list

 elif action == "2":
card_list.remove(find_dict)
​
print(" Delete successful ")
  • Because the dictionary record found has been saved in the list

  • To change the business card record , Just modify the data of each key value pair in the corresponding dictionary in the list

 if action == "1":
​
find_dict["name"] = input(" Please enter a name :")
find_dict["phone"] = input(" Please input the phone number :")
find_dict["qq"] = input(" Please enter QQ:")
find_dict["email"] = input(" Please enter email :")
​
print("%s The business card of was modified successfully " % find_dict["name"])
​

If the user is using , Some business card contents don't want to be modified , How to do it ?—— Since the system provides  input  The function does not satisfy the requirement , So define a new function  input_card_info  To the system  input  Function to expand

def input_card_info(dict_value, tip_message):
​
""" Enter business card information
​
:param dict_value: Dictionary original value
:param tip_message: Enter the prompt message
:return: If input , Return to input content , Otherwise, return the original dictionary value
"""
# 1. Prompt the user to enter content
result_str = input(tip_message)
​
# 2. Judge the user's input , If the user enters content , Direct return
if len(result_str) > 0:
​
return result_str
# 3. If the user does not enter , return ` The original value in the dictionary `
else:
​
return dict_value
​

 

 

6. Give Way python The program runs directly

cards_input.py

def input_card_info(dict_value, tip_message):
""" Enter business card information
:param dict_value: The original value in the dictionary
:param tip_message: Enter the prompt text
:return: If the user enters content , Just return the content , Otherwise, return the original value in the dictionary
"""
# 1. Prompt the user to enter content
result_str = input(tip_message)
# 2. Judge the user's input , If the user enters content , Direct return
if len(result_str) > 0:
return result_str
# 3. If the user does not enter , return ` The original value in the dictionary `
else:
return dict_value

cards_main.py

#!/usr/bin/python3
import cards_tools
while True:
cards_tools.show_menu()
action = input(" Please select operation function :")
print(" The operation you choose is :%s" % action)
# Determine the subsequent operations according to the user's input
if action in ["1", "2", "3"]:
if action == "1":
cards_tools.new_card()
elif action == "2":
cards_tools.show_all()
elif action == "3":
cards_tools.search_card()
elif action == "0":
print(" Welcome to use again 【 Business card management system 】")
break
else:
print(" Input error , Please re-enter :")

cards_tools.py

import cards_input
# A list of all business card records
card_list = []
def show_menu():
""" Show menu
"""
print("*" * 50)
print(" Welcome to use 【 Business card management system 】V1.0")
print("")
print("1. New business card ")
print("2. Show all ")
print("3. Check the business card ")
print("")
print("0. Exit the system ")
print("*" * 50)
def new_card():
""" New business card
"""
print("-" * 50)
print(" function : New business card ")
# 1. Prompt the user to input the business card information
name = input(" Please enter a name :")
phone = input(" Please input the phone number :")
qq = input(" Please enter QQ number :")
email = input(" Please enter email address :")
# 2. Save user information to a dictionary
card_dict = {"name": name,
"phone": phone,
"qq": qq,
"email": email}
# 3. Add user dictionary to business card list
card_list.append(card_dict)
# print(card_list)
# 4. Prompt to add success message
print(" Successful addition %s Business card of " % card_dict["name"])
def show_all():
""" Show all
"""
print("-" * 50)
print(" function : Show all ")
# 1. Judge if there is a business card record
if len(card_list) == 0:
print(" Tips : There are no business card records ")
return
# 2. Show all business cards
print(" full name \t\t Telephone \t\tQQ\t\t mailbox ")
print("-" * 60)
for card_dict in card_list:
print("%s\t\t%s\t\t%s\t\t%s" % (
card_dict["name"],
card_dict["phone"],
card_dict["qq"],
card_dict["email"]))
print("-" * 60)
def search_card():
""" Search for business cards
"""
print("-" * 50)
print(" function : Search for business cards ")
# 1. Prompt for names to search
find_name = input(" Please enter the name you want to search :")
# 2. Ergodic dictionary
for card_dict in card_list:
if card_dict["name"] == find_name:
print(" full name \t\t Telephone \t\tQQ\t\t mailbox ")
print("-" * 60)
print("%s\t\t%s\t\t%s\t\t%s" % (
card_dict["name"],
card_dict["phone"],
card_dict["qq"],
card_dict["email"]))
print("-" * 60)
deal_card(card_dict)
break
else:
print(" Can't find %s" % find_name)
def deal_card(find_dict):
""" Operate the business card dictionary found
:param find_dict: Business card Dictionary
"""
action_str = input(" Please enter the operation of the business card :1: modify / 2: Delete / 0: Go back to the upper menu ")
if action_str == "1":
find_dict["name"] = cards_input.input_card_info(find_dict["name"],
" Please enter a name [ Press enter without modification ]:")
find_dict["phone"] = cards_input.input_card_info(find_dict["phone"],
" Please input the phone number [ Press enter without modification ]:")
find_dict["qq"] = cards_input.input_card_info(find_dict["qq"],
" Please enter QQ[ Press enter without modification ]:")
find_dict["email"] = cards_input.input_card_info(find_dict["email"],
" Please enter email address [ Press enter without modification ]:")
print("%s The business card of was modified successfully !" % find_dict["name"])
elif action_str == "2":
card_list.remove(find_dict)
print(" Delete business card successfully !")

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25