zoukankan      html  css  js  c++  java
  • Python学习笔记(八)- 名片管理系统-增删改查等基础操作

    代码目标:

    实现一个基础的名片管理系统。

    实现以下功能:

    1、输入判断;

    2、增添新的名片;

    3、删除名片;

    4、修改已有名片;

    5、查询给定信息的名片。

    cards_main.py 的代码:

     1 import tools
     2 
     3 # pass  # pass占位符,如果不需要立刻编写的话,可以使用pass保证程序代码结构正确。
     4 
     5 while True:
     6     tools.menu()  # 显示功能菜单
     7     str = input("Please choose operation:")
     8     print("Your choice is [%s]" % str)
     9 
    10     # 输出123是名片的操作,0退出系统
    11     if str in ["1", "2", "3"]:
    12         if str == "1":  # todo add
    13             tools.new_card()
    14         elif str == "2":  # todo display all
    15             tools.show_all()
    16         else:  # todo query
    17             tools.serch_card()
    18     elif str == "0":
    19         print("Goodbye!")
    20     else:
    21         print("Your choice is wrong,please again!")

    tools.py的代码(增删改查的基础操作):

     1 card_list = []  # 所有名字字典
     2 
     3 
     4 def menu():
     5     """
     6     显示菜单
     7     """
     8     print("*" * 50)
     9     print("Welcome!")
    10     print("")  # 输出空行
    11     print("1. 新增名片")
    12     print("2. 显示全部")
    13     print("3. 搜索名片")
    14     print("")
    15     print("0. 退出系统")
    16     print("*" * 50)
    17 
    18 
    19 def new_card():
    20     print("-" * 50)
    21     print("新增名片")
    22     name = input("Please enter your name")
    23     phone = input("Please enter your phone")
    24     card_dict = {"name": name,
    25                  "phone": phone}
    26     card_list.append(card_dict)
    27     print("添加 %s 的姓名成功" % name)
    28 
    29 
    30 def show_all():
    31     print("-" * 50)
    32     print("显示所有名片")
    33 
    34     # 判断是否存在名片记录
    35     if len(card_list == 0):
    36         print("当前没有任何名片记录,请添加")
    37         return
    38 
    39     # 打印表头
    40     for name in ["NAME", "PHONE"]:
    41         print(name, end="	")
    42     print("")
    43     print("=" * 50)
    44     for i in card_list:
    45         print("%s		%s	" % (i["name"], i["phone"]))
    46 
    47 
    48 def serch_card():
    49     print("-" * 50)
    50     print("搜索名片")
    51     # 提示用户需要搜索的姓名
    52     find_name = input("请输入要搜索的姓名: ")
    53     for i in card_list:
    54         if i["name"] == find_name:
    55             print("姓名		电话")
    56             print("=" * 50)
    57             print("%s		%s	" % (i["name"], i["phone"]))
    58 
    59             # 处理已经找到的名片(修改、删除)
    60             del_card(i)
    61             break
    62     else:  # 不是通过break退出的将会执行此句代码
    63         print("抱歉,没有找到 %s !" % find_name)
    64 
    65 
    66 def del_card(find_dict):
    67     print(find_dict)
    68     str = input("Please choose your choice: "
    69                 "1.modify 2.delete 3.return ")
    70     if str == "1":
    71         find_dict["name"] = input_change(find_dict["name"], "name: ")
    72         find_dict["phone"] = input_change(find_dict["phone"], "phone: ")
    73         print("modify successed !")
    74     elif str == "2":
    75         print("delete successed !")
    76         card_list.remove(find_dict)
    77     else:
    78         return
    79 
    80 
    81 def input_change(value, tips):
    82     str = input(tips)
    83     if len(str) > 0:
    84         return str  # 修改的值
    85     else:
    86         return value  # 原本的值
  • 相关阅读:
    RabbitMQ插件安装
    RabbitMQ安装与初始配置
    Spring Boot教程(四十二)LDAP来管理用户信息(2)
    Spring Boot教程(四十一)LDAP来管理用户信息(1)
    Spring Boot教程(四十)使用Flyway来管理数据库版本
    Spring Boot教程(三十九)使用MyBatis注解配置详解(2)
    Spring Boot教程(三十八)使用MyBatis注解配置详解(1)
    Spring Boot教程(三十七)整合MyBatis
    Spring Boot教程(三十六)使用MongoDB数据库(2)
    Spring Boot教程(三十五)使用MongoDB数据库(1)
  • 原文地址:https://www.cnblogs.com/OFSHK/p/12956847.html
Copyright © 2011-2022 走看看