初学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]()

如有错误,或者更好的想法,请指教。

【python】——小程序之电话薄的更多相关文章

  1. 怎么样通过编写Python小程序来统计测试脚本的关键字

    怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...

  2. Python 小程序,对文件操作及其它

    以下是自己写的几个对文件操作的小程序,里面涉及到文件操作,列表(集合,字典)的运用等.比方说,从文件里读取一行数据.分别存放于列表中,再对列表进行操作.如去掉里面的反复项.排序等操作. 常见对文件里行 ...

  3. Day1:第一个python小程序

    Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...

  4. 一个有意思的Python小程序(全国省会名称随机出题)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 最近比较迷Python,仿照<Python编程快速上手>8.5写了一个随机出卷的小 ...

  5. 【转】我的第一个Python小程序

    原文网址:http://blog.csdn.net/randyqiu/article/details/4484089 人的每个第一次都有点特别的意义,所以下面这个小程序我把他记录下来做个纪念. 因为要 ...

  6. 人人都可以写的可视化Python小程序第二篇:旋转的烟花

    兴趣是最好的老师 枯燥的编程容易让人放弃,兴趣才是最好的老师.无论孩子还是大人,只有发现这件事情真的有趣,我们才会非常执着的去做这件事,比如打游戏.如果编程能像玩游戏一样变得有趣,我相信很多人就特别愿 ...

  7. Appium + python -小程序实例

    from appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionfrom time im ...

  8. 写一个python小程序

    在windows环境下进行操作 window+R 输入cmd  创建一个文件夹 mkdir pytxt 创建一个py文件 py.py  用notepad或者记事本等工具进行编辑 或 首先声明pytho ...

  9. 自己写的一个抢票加速的Python小程序源码分享-----纯属娱乐

    最近这段时间频频看到微信群里发什么 抢票加速,智行.携程.飞猪.美团,对于我这能坐客车就不坐火车的人来说,无所谓靠谱不靠谱 突发奇想的整理了下整个抢票加速的逻辑,写了这个小程序,代码很low,拒绝批评 ...

随机推荐

  1. 1.2 Broadcast

    在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件. 一个Broad ...

  2. elixir 高可用系列(二) GenServer

    概述 如果我们需要管理多个进程,那么,就需要一个专门的 server 来集中监控和控制这些进程的状态,启停等. OTP 平台中的 GenServer 就是对这个 server 通用部分的抽象. 利用 ...

  3. IP协议

    因特网协议(Internet Protocol, IP)是Internet的核心协议之一, 工作在网络层.IP协议提供节点间的寻址,路由以及顺序控制, 流量控制等服务. IP协议分为IPv4和IPv6 ...

  4. node-webkit教程(10)Platform Service之File dialogs

    node-webkit教程(10)Platform Service之File dialogs 文/玄魂 目录 node-webkit教程(10)Platform Service之File dialog ...

  5. 高手速成android开源项目【导航篇】

    Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的智慧和创造力.学习开源代码是掌握技术的一个 ...

  6. [WinAPI] API 6 [操作驱动器挂载点]

    驱动器挂载点,又可以称作卷挂载点.挂载点实际上是操作系统或者用户设置的,用来进入一个逻辑驱动器或者卷的入口.在设置了卷的挂载点后,用户或者应用程序可以使用卷标或者指定的挂载点来进入卷.比如在“C:\” ...

  7. JfreeChart使用(转载)

    http://www.cnblogs.com/xingyun/ http://www.huosen.net/archives/156.html(此篇除了struts2外,还介绍了servlet下Jfr ...

  8. Nagios学习笔记三:配置Nagios

    1.Nagios的主配置文件 Nagios的主配置文件为nagios.cfg,其语法非常简洁,通常#开头的行为注释行,而参数的设置格式为<parameter>=<value>: ...

  9. 对TCP说三道四

    夜朦胧,人方静,无聊的人打开了无聊的电脑看到了一张无聊的图,想着想着就睡着了,梦到了人a和人b的一次聊天.        有一天,a有事情想跟b商量就问b"有时间么,想和你聊一下天" ...

  10. 比较用decodeFileDescriptor和decodeFile的区别

    从本地中读取图片,可以用decodeFileDescriptor和decodeFile,至于哪一种方式的耗内存情况作了一次简单对比,可能一次选取6张图片数量过少,貌似区别不大,decodeFileDe ...