python学习

http://woodpecker.org.cn/abyteofpython_cn/chinese/index.html

python基础知识看完了,对python的基本语法是简单的用法有了较浅显的理解。

第一个python程序

#! /usr/bin/env python
# The address module

import cPickle as p

class person:
    '''This is my first python programme.

    It will realize the person class etc.'''
    def __init__(self,tn,adr,email):
        if len(tn)< 7:
            print 'Telephonenumber is too short'
            self.telephonenumber=0
        else:
            self.telephonenumber=tn
        if len(adr)<0:
            print 'address is wrong'
            self.address=''
        else:
            self.address=adr
        if len(email)<0:
            print 'email is wrong'
            self.email=''
        else:
            self.email=email
    def modify_telephonenumber(self,tn):
        if len(tn)< 7:
            print 'Telephonenumber is too short'
        else:
            self.telephonenumber=tn
    def modify_address(self,adr):
        if len(adr)<0:
            print 'address is wrong'
        else:
            self.address=adr
    def modify_email(self,email):
        if len(email)<0:
            print 'email is wrong'
        else:
            self.email=email
    def printall(self):
        print 'Telephonenumber is %s' %self.telephonenumber
        print 'Address is %s' %self.address
        print 'Email is %s' %self.email
        

        
class myaddress:
    '''This is my first python programme.

    It will realize address class etc.'''
    count = 0
    def __init__(self):
        '''initiator the class'''
        myaddress.count+=1
        self.addressbook={}
        
    def add_address(self,key,value):
        self.addressbook[key] = value
        
    def delete_address(self,key):
        del self.addressbook[key]
        
    def printinfo(self,key):
        print 'Name is',key
        self.addressbook[key].printall()
        
    def printinfoall(self):
        for key in self.addressbook:
            print 'Name is',key
            self.addressbook[key].printall()
        
    def __del__(self):
        pass
       



def process(iam):
    while True:
        print '''Enter the infomaiton you want to save (if you want quit you can enter 'quit') '''
        name = raw_input('Enter the name you want to save : ')
        if name == 'quit':
            break
        tel = raw_input('Enter the telephonenumber you want to save : ')
        if tel == 'quit':
            break
        address = raw_input('Enter the address you want to save : ')
        if address == 'quit':
            break
        email = raw_input('Enter the email you want to save : ')
        if email == 'quit':
            break
        info = person(tel,address,email)
        iam.add_address(name,info)
        isgoon = raw_input('Is go on ? Y/N: ')
        if isgoon == 'N':
            break
    
    
def storeinfo(savefile,iam):   
    f = file(savefile,'w')
    p.dump(iam,f)
    f.close()

def loadinfo(savefile):
    f = file(savefile)
    iam = p.load(f)
    return iam
    


iam = myaddress()
process(iam)
savefilename = raw_input('Enter the filename you want to use to save : ')
storeinfo(savefilename,iam)

del iam
iam = loadinfo(savefilename)
iam.printinfoall()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值