个人感觉学习一门新语言,简单的语法懂一点足矣。接下来就是编程。读懂别人程序的每一句,理解每一句的意义。

#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的更多相关文章

  1. 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代

    2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...

  2. Angular2学习笔记(1)

    Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...

  3. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  4. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  5. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  6. Unity3d学习 制作地形

    这周学习了如何在unity中制作地形,就是在一个Terrain的对象上盖几座小山,在山底种几棵树,那就讲一下如何完成上述内容. 1.在新键得项目的游戏的Hierarchy目录中新键一个Terrain对 ...

  7. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  8. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

  9. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

随机推荐

  1. Soufun_News

    using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.ComponentMod ...

  2. qt 程序启动参数 -qws  (转至 MrTXK

    运行嵌入式程序 在嵌入式QT版本中,程序需要服务器或自己作为服务器程序.服务器程序构造的方法是构造一个QApplication::GuiServe类型的QApplication对象.或者使用-qws命 ...

  3. phonegap 框架详解

    首先, 来看一下phonegap 初始化流程以及Native 与 JS 交互流程图. 说明:socket server模式下, phonegap.js 源码实现的采用1 毫秒执行一次XHR请求,  当 ...

  4. zip函数

    zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个包含元组的列表. x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) ...

  5. HDU 4576 简单概率 + 滚动数组DP(大坑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: #include <cstdio> #includ ...

  6. C# winform应用程序仅能打开一个进程运行

    判断程序是否已经运行,使程序只能运行一个实例: 方法1: //这种检测进程的名的方法,并不绝对有效.因为打开第一个实例后,将运行文件改名后,还是可以运行第二个实例. private static bo ...

  7. C#中用schema验证xml的合法性

    class ValidateXML { public string ErrString = string.Empty; public void ValidationEventCallBack(Obje ...

  8. 8UFTP

    FTP  文件上传下载工具    FTP(File Transfer Protocol)是Internet上用来传送文件的协议(文件传输协议). 官网:http://ftp.8u.cn 网盘下载(版本 ...

  9. 【 D3.js 入门系列 --- 2 】 如何使用数据和选择元素

    接着上一讲的内容,这次讨论如何选择元素和使用数据.    现在页面中有三行文字,代码为: <p>Hello World 1</p> <p>Hello World 2 ...

  10. uget和aria2

    http://blog.csdn.net/luojiming1990/article/details/9078447 其中的aria2 -v要改成aria2c -v