[Python] business card management system

Card management system

aims

Comprehensive already learned knowledge points

  • variable
  • Process Control
  • function
  • Module
    Development card management system

Frame structures

Framework program is that the program can simply run, but the program is far from perfect, have a basic prototype

  • Build business card management system framework
    1. Preparation of documents , determine the file name, guaranteed in the desired position to write code
    2. Preparation of the main run loop , the basic user input and Analyzing

      Document preparation

  1. New cards_main.pysave main function code
    • Entrance Program
    • Each time you start a business card management systems through the mainstart of this document
  2. New cards_tools.pysave all the business card performance function
    • The cards will add, query, modify, delete and other functions packaged in a different function

Write the main run loop

  • In cards_mainadd one infinite loop
while True:
    # TODO(Rowry)显示功能菜单


    action_str = input("请选择希望执行的操作: ").strip()
    print("您选择的操作是 [%s]" % action_str)

    # 1,2,3 针对名片进行操作
    if action_str in ["1", "2", "3"]:
        # 新增名片
        if action_str == "1":
            pass
        # 显示全部
        elif action_str == "2":
            pass
        # 查询名片
        else:
            pass

        # 在开发程序时,如果不希望立刻编写 => pass关键字
        # pass
    # 0 退出系统
    elif action_str == "0":
        print("欢迎再次使用 <名片管理系统>")
        break
    # 其他内容输入错误,需要提示用户
    else:
        print("您输入的不正确,请重新选择")

cards_main knowledge Summary

Analyzing string

if action in ["1","2","3"]:
if action =="1" or action == "2" or action =="3":
  • Use infor the list is determined to avoid the use of orsplicing complex logic conditions
  • Do not use intthe conversion input from the user can be avoided once the user is not a number , resulting in run error => str generally entered, then the conversion of

pass

  • passIs an empty statement does nothing, commonly used as a placeholder sentence
  • In order to maintain the integrity of the program structure => Written procedures overall framework can be used pass

Infinite loop

  • When developing the software, if you do not want the program to perform an immediate exit, you can add a program in an infinite loop , it is determined by the user to exit the program opportunity

TODO comment

  • In #later to keep up TODO, to mark the work needs to be done
# TODO(作者/邮件) 显示系统菜单

In cards_toolsadded four new functions

def show_menu():

    """显示菜单
    """
    pass

def new_card():

    """新建名片
    """
    print("-" * 50)
    print("功能:新建名片")


def show_all():

    """显示全部
    """
    print("-" * 50)
    print("功能:显示全部")


def search_card():

    """搜索名片
    """
    print("-" * 50)
    print("功能:搜索名片")

Import module

  • In cards_main.pyuse importimport cards_toolsmodule
  • The modified code as follows
import cards_tools

while True:
    # 显示功能菜单
    cards_tools.show_menu()

    action_str = input("请选择希望执行的操作: ").strip()
    print("您选择的操作是 [%s]" % action_str)

    # 1,2,3 针对名片进行操作
    if action_str in ["1", "2", "3"]:
        # 新增名片
        if action_str == "1":
            cards_tools.new_card()
        # 显示全部
        elif action_str == "2":
            cards_tools.show_all()
        # 查询名片
        else:
            cards_tools.search_card()

        # 在开发程序时,如果不希望立刻编写 => pass关键字
        # pass
    # 0 退出系统
    elif action_str == "0":
        print("欢迎再次使用 <名片管理系统>")
        break
    # 其他内容输入错误,需要提示用户
    else:
        print("您输入的不正确,请重新选择")

At this point: cards_mainall code development all finished!

Complete show_menufunction

def show_menu():

    """显示菜单
    """
    print("*" * 50)
    print("欢迎使用【菜单管理系统】V1.0")
    print("")
    print("1. 新建名片")
    print("2. 显示全部")
    print("3. 查询名片")
    print("")
    print("0. 退出系统")
    print("*" * 50)

Save the business card data structure

Procedure is used to process the data, and the variable is used to store data

  • Use the dictionary record every business card details
  • Use the list of unified record of all business cards dictionary

List of variables defined business card

  • In cards_toolsincreasing the evil one top of the file list of variables
# 所有名片记录的列表
card_list = []

note:

  1. All card-related operations , we need to use this list, so it should be defined at the top of the program
  2. Just run the program, there is no data , so when an empty list

New business card

Functional Analysis

  1. In turn prompts the user to enter contact information
  2. To save the business card information to a dictionary
  3. Add the dictionary to your contacts list
  4. Tip addition was complete card

achievenew_card()

def new_card():
    """
    新增名片
    :return:
    """
    # 1. 提示用户依次输入名片信息
    name_str = input("请输入姓名: ").strip()
    phone_str = input("请输入电话: ").strip()
    qq_str = input("请输入QQ: ").strip()
    email_str = input("请输入邮箱: ").strip()
    # 2. 将名片信息保存到一个字典
    card_dict = {"name":name_str,
                 "phone":phone_str,
                 "qq":qq_str,
                 "email":email_str}
    # 3. 将字典添加到名片列表
    card_list.append(card_dict)
    print(card_list)
    # 4. 提示名片添加完成
    print("添加 %s 的名片成功! "% name_str)

Tip: In the IDE Refactor => Rename => name can quickly modify variables unity

Show all cards

Functional Analysis

  • Loop through the contacts list, the display order information of each dictionary
def show_all():
    """
    显示所有名片
    :return:
    """
    print("-" * 50)
    print("功能: 显示所有名片")

    # 判断是否存在名片记录,如果没有,提示用户并返回
    if len(card_list) == 0:
        print("当前没有任何名片记录,请使用新增功能添加卡片!")
        # => 也是使用return提前结束
        return

    # 打印表头
    for name in ["姓名", "电话", "QQ", "邮箱"]:
        print(name, end="\t\t")
    print()
    # 打印分割线
    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"]))

note:

  • In the function returnwhich returns
  • If returnthe meters have with anything, but said that the implementation of this function will no longer perform the subsequent code, return to the calling function at

Discover Card

Functional Analysis

  1. Prompts the user to search for names
  2. According to user input through a list of names
  3. After the specified search card, and then performs subsequent operations
  • Query functions to achieve
def search_card():
    """
    搜索名片
    :return:
    """
    print("-" * 50)
    print("功能: 搜索名片")

    # 1. 提示用户输入要搜索的姓名
    find_name = input("请输入要搜索的姓名: ")

    # 2. 遍历名片列表,查询要搜索的姓名
    # 这里利用了 for-else 的语法
    for card_dict in card_list:
        if card_dict["name"] == find_name:
            print("姓名\t\t电话\t\tQQ\t\t邮箱")
            print("=" * 50)
            print(
                "%s\t\t  %s \t\t %s \t\t %s" % (
                    card_dict["name"], card_dict["phone"], card_dict["qq"], card_dict["email"]))

            deal_card(card_dict)
            break
    else:
        print("抱歉,没有找到 %s 的相关信息" % find_name)
  • Add contacts manipulation functions: modify / delete / return to the main menu
def deal_card(find_card):
    """
    处理查找到的名片
    :param find_card: 查找到的名片
    :return:
    """
    action_str = input("请选择要执行的操作"
                       " [1] 修改 [2] 删除 [0] 返回上级菜单").strip()
    if action_str == "1":
        # 这里的函数值传递 => 对象引用
        find_card["name"]=input_card_info(find_card["name"],"修改姓名为[回车不修改]: ")
        find_card["phone"]=input_card_info(find_card["phone"],"修改电话为[回车不修改]: ")
        find_card["qq"]=input_card_info(find_card["qq"],"修改QQ为[回车不修改]: ")
        find_card["email"]=input_card_info(find_card["email"],"修改邮箱为[回车不修改]: ")
        print("修改名片成功!")

    elif action_str == "2":
        # 直接这样就可以删除列表中的字典
        card_list.remove(find_card)
        print("已经成功删除 %s 的信息!" % find_card["name"])
  • Refine and edit contact cards
    • If the user is in use, some do not want to modify the card content => System provided input()can not meet the demand, then define a new function input_card_info()of the system input()be extended
    def input_card_info(dict_value,tip_message):
      """
      系统的input()不满足需求,针对该名片管理系统设计的输入函数
      :param dict_value: 字典中原有的值
      :param tip_message: 输入的提示文字
      :return: 如果用户输入了内容,就返回内容,否则返回字典中原有的值
      """
      # 1. 提示用户输入内容
      result_str = input(tip_message).strip()
      # 2. 针对用户的输入进行判断,如果用户输入了内容,直接返回结果
      if len(result_str) > 0:
          return result_str
      # 3. 如果用户没有输入内容,返回字典中原有的值
      else:
          return dict_value

Linux on the Shebangsymbol ( #!)

  • #!This symbol is called ShebangorSha-bang
  • ShebangUsually in the unixsystem script at the beginning of the first line use
  • Role: Indicates execute this script file interpreter

Use Shebang steps

  1. Use whichthe query python3path where the interpreter
$ which python3
  1. To run the modified main python file , add the following line in a first
#! /usr/bin/python3
  1. Modify the main python file file permissions, increased execute permissions
$ chmod +x cards_main.py
  1. Program can be executed when needed
./cards_main.py

Guess you like

Origin www.cnblogs.com/Rowry/p/11826663.html