pytion学习1
个人感觉学习一门新语言,简单的语法懂一点足矣。接下来就是编程。读懂别人程序的每一句,理解每一句的意义。
#Filename:MyAddressBook.py
import cPickle as p
import os
#Class Item
class Item:
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
#the main menu of address book
def menu():
'''the main menu of address book'''
print ''
print '1.Insert an item'
print '2.Delete an item'
print '3.Modify an item'
print '4.Display all items'
print '5.Sort all items'
print '6.Exit the program'
print 'What do you want to do?'
#initialization of system, load the member list
def begin():
'''initialization of system, load the member list'''
global itemlist
if os.path.exists('memberlist.data') == True:#to judge whether the file exists
listfile = file('memberlist.data','r')
if len(listfile.read())!=0:#to judge whether the file is empty
itemlist = p.load(listfile)
listfile.close()
#exitance of system, store the menber list
def end():
'''exitance of system, store the menber list'''
global itemlist
listfile = file('memberlist.data','w+')
p.dump(itemlist,listfile)
listfile.close()
#insert an item into the member list
def insert():
'''insert an item into the member list'''
name = raw_input('Enter name:')
age = int(raw_input('Enter age:'))
gender = raw_input('Enter gender:')
item = Item(name,age,gender)
global itemlist
itemlist.append(item)
#print an item
def output(item):
'''print an item'''
print '%-15s%-5d%s'%(item.name,item.age,item.gender)
#print all items
def display():
'''print all items'''
global itemlist
l = len(itemlist)
print 'name age gender'
for i in range(0,l):
output(itemlist[i])
print ''
#delete an item by name from member list
def delete():
'''delete an item by name from member list'''
name = raw_input('Enter the name you want to delete:')
global itemlist
l = len(itemlist)
for i in range(0,l):
if (itemlist[i].name == name):
itemlist.pop(i)
#update an item
def update(item):
'''update an item'''
item.name = raw_input('Enter name:')
item.age = int(raw_input('Enter age:'))
item.gender = raw_input('Enter gender:')
#update an item's information by name
def modify():
'''update an item's information by name'''
name = raw_input('Enter the name you want to modify:')
global itemlist
l = len(itemlist)
for i in range(0,l):
if (itemlist[i].name == name):
update(itemlist[i])
print 'Update done!'
#sort all items by name
def sort():
global itemlist
itemlist.sort(None,key = lambda item:item.name)
#Here are the scripts
itemlist = [] #Notice here!!!
begin()
while True:
menu()
sel = int(raw_input())
if sel == 1:
insert()
elif sel == 2:
delete()
elif sel == 3:
modify()
elif sel == 4:
display()
elif sel == 5:
sort()
else:
break
end()
print 'Good Bye!'
pytion学习1的更多相关文章
- 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代
2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Unity3d学习 制作地形
这周学习了如何在unity中制作地形,就是在一个Terrain的对象上盖几座小山,在山底种几棵树,那就讲一下如何完成上述内容. 1.在新键得项目的游戏的Hierarchy目录中新键一个Terrain对 ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- 菜鸟Python学习笔记第一天:关于一些函数库的使用
2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
随机推荐
- Linux驱动之HelloWorld
最近看android的一些源码,里面有一些功能是用驱动实现的.于是就兴起看了一些驱动相关的东西,准备日后深入.这没有技术含量的水文,仅作为日后的备忘吧. 系统使用的是ubuntu 12.0.04,内核 ...
- 配置ConvenientBanner时出现的问题
ConvenientBanner: compile 'com.bigkoo:convenientbanner:2.0.5' 找不到依赖库 classpath 'com.jfrog.bintray.gr ...
- androidTV第一次创建(转:支持原创)
转载地址:http://blog.csdn.net/aa2967277/article/details/50617677 AndroidTV应用开发简介 目前,网上还没有对AndroidTV的足够的介 ...
- Linux的管道
一.管道是什么? 管道,顾名思义就是个管子,里面可以流过去很多东西.举个栗子 ls | morels输出列出来的文件目录就通过‘|’这个管道流向了more这个文本浏览器.相同的功能我们也可以通过ls ...
- OD调试篇9
渐渐地要用比较高明一点的方法去破解软件了 那好,看看今天的程序先 先载入 测试下程序 发现这是一个未注册版本的程序,注册也不让注册,注册就跳出You have rntered an invalid ...
- ACE - ACE_Task源码剖析及线程池实现
原文出自http://www.cnblogs.com/binchen-china,禁止转载. 上篇提到用Reactor模式,利用I/O复用,获得Socket数据并且实现I/O层单线程并发,和dispa ...
- swiper的使用
最近要用html5制作可以一屏一屏向上滑动的页面,发现大家使用swiper插件的较多,所以试了试,发现做出来的效果还不错,喜欢的朋友可以参考一下自己动手做啦. 1.首先我们要引入4个文件: <h ...
- Why NSAttributedString import html must be on main thread?
The HTML importer should not be called from a background thread (that is, the options dictionary inc ...
- YHLMR009 交货单查询
*********************************************************************** * Title : YHLMR009 * * Appli ...
- redis windows
下载 redis windows版本 redis-server redis.windows.conf //cmd 命令 启动成功 E:\redis\Redis-x64-3.0.500>red ...