• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python编写电话薄实现增删改查功能

Python编写电话薄实现增删改查功能

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python编写软件,python编写手机软件,python脚本编写实例,python编写测试脚本,python编写的小游戏等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

初学python,写一个小程序练习一下。主要功能就是增删改查的一些功能。主要用到的技术:字典的使用,pickle的使用,io文件操作。代码如下:

import pickle

#studentinfo = {'netboy': '15011038018',\
#                'godboy': '15011235698'}
studentinfo = {}

FUNC_NUM = 5

def write_file(value):
    file = open('student_info.txt', 'wb')
    file.truncate()
    pickle.dump(value, file, True)
    file.close

def read_file():
    global studentinfo
    file = open('student_info.txt', 'rb')
    studentinfo = pickle.load(file)
    file.close()

def search_student():
    global studentinfo
    name = input('please input student\'s name:')
    if name in studentinfo:
        print('name:%s phone:%s' % (name, studentinfo[name]))
    else:
        print('has no this body')

def delete_student():
    global studentinfo
    name = input('please input student\'s name:')
    if name in studentinfo:
        studentinfo.pop(name)
        write_file(studentinfo)
    else:
        print('has no this body')

def add_student():
    global studentinfo
    name = input('please input student\'s name:')
    phone = input('please input phone:')
    studentinfo[name] = phone
    write_file(studentinfo)

def modifiy_student():
    global studentinfo
    name = input('please input student\'s name:')
    if name in studentinfo:
        phone = input('please input student\'s phone:')
        studentinfo[name] = phone
    else:
        print('has no this name')

def show_all():
    global studentinfo
    for key, value in studentinfo.items():
        print('name:' + key + 'phone:' + value)

func = {1 : search_student, \
    2 : delete_student, \
    3 : add_student, \
    4 : modifiy_student, \
    5 : show_all}

def menu():
    print('-----------------------------------------------');
    print('1 search student:')
    print('2 delete student:')
    print('3 add student:')
    print('4 modifiy student:')
    print('5 show all student')
    print('6 exit')
    print('-----------------------------------------------');

def init_data():
    global studentinfo
    file = open('student_info.txt', 'rb')
    studentinfo = pickle.load(file)
    #print(studentinfo)
    file.close()

init_data()
while True:
    menu()
    index = int(input())
    if index == FUNC_NUM + 1:
        exit()
    elif index < 1 or index > FUNC_NUM + 1:
        print('num is between 1-%d' % (FUNC_NUM + 1))
        continue
    #print(index)
    func[index]()
</div>

以上就是本文的全部内容,希望对大家学习Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python编写简单的HTML页面合并脚本
  • Python编写简单的HTML页面合并脚本
  • Python编写电话薄实现增删改查功能
  • 编写Python的web框架中的Model的教程
  • 编写Python的web框架中的Model的教程

相关文章

  • 浅谈python中的面向对象和类的基本语法
  • django 自定义用户user模型的三种方法
  • Python的SQLAlchemy框架使用入门
  • 用Python编写一个国际象棋AI程序
  • 详解Django中的过滤器
  • 浅析Python中的多进程与多线程的使用
  • Python的SQLalchemy模块连接与操作MySQL的基础示例
  • Python脚本文件打包成可执行文件的方法
  • 在Django的session中使用User对象的方法
  • Python 装饰器深入理解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • win10系统中安装scrapy-1.1
    • python类参数self使用示例
    • Python的多态性实例分析
    • Python中遇到的小问题及解决方法汇总
    • Python的SQLalchemy模块连接与操作MySQL的基础示例
    • python之文件的读写和文件目录以及文件夹的操作实现代码
    • python计算圆周率pi的方法
    • python查找第k小元素代码分享
    • Python使用MySQLdb for Python操作数据库教程
    • python监控网站运行异常并发送邮件的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有