白菜粉条汤
make life remembered
  • 首页
  • 分类
    • 随语
    • 杂志
    • 外贸
    • 技术
    • 图片
    • 电影
    • 音乐
    • 饮食
    • 大学
  • 关于
Tag:

简单电话薄

技术

python程序练习–简单电话薄

by 曾经沧海 2009年5月18日

python程序练习–简单电话薄代码

#python程序练习–简单电话薄 
#樊海军 
#fanhaijun.com 
#Python 2.6.2 

import cPickle as p 
import string 
class phonebook: 
        def __init__(self): 
                pblist=[] 

def pb_add(): 
        #print "This is the ADD function" 
        while 1: 
                print ‘\nPlease input a NEW name["Q" to back to main menu]:\n’ 
                add_name=raw_input("Name:") 

                if add_name=="Q": 
                        main() 
                else: 
                        add_sex=raw_input("Sex:") 
                        add_tel=raw_input("Tel:")                       
                pbdict={"Name":add_name,"Sex":add_sex,"Tel":add_tel}            
                print "You just input:" 
                for i in pbdict.items(): 
                        print ("%s:%-5s  ") % (i[0],i[1]), 

                pb.pblist.append(pbdict) 
                del pbdict 
             print ("\nRecord added sucessfully! Continue?(Y/N)\n")              
        while 1: 
                       con=raw_input() 
            if con=="Y": 
                            pb_add() 
                    elif  con=="N": 
                            pb_save() 
                            main() 
            else: 
                print ("\nError! Continue?(Y/N)\n") 

def pb_browser(): 
        #print "This is the BROWSER function" 
        #print pb.pblist 

        print "\nThere are %s records:\n"% len(pb.pblist) 
        print "*******************************************" 
        print "  ID     NAME     SEX      TEL" 
        print "*******************************************" 
    for i in pb.pblist: 
               #print "===========================================" 
                print "%3s     " % (pb.pblist.index(i)+1), 
                print "%-8s" % i["Name"], 
                print "%-8s" % i["Sex"], 
                print "%-8s" % i["Tel"] 
            print "===========================================" 
        main()  

def pb_del(): 
        #print "This is the DEL function" 

        while 1: 
                print "\nPlease input the ID of the record you want to delete:[Input 0 to main menu]\n" 
                del_id=raw_input() 
                if int(del_id)==0: 
                        main() 
                elif del_id=="" or int(del_id)<0 or int(del_id)>len(pb.pblist): 
                        #print "\nPlease input a RIGHT ID :\n"   
                        continue 

                elif int(del_id)<=len(pb.pblist) and int(del_id)>0: 
                        del pb.pblist[int(del_id)-1] 
            pb_save() 

def pb_search(): 
        #print "This is the SEARCH function" 

    while 1: 
        print "Please input what ever you want to search:" 
        s=str(raw_input()).lower() 
        k=0 
        id_list=[] 
        for i in pb.pblist: 
            if s in i["Name"].lower() or s in i["Sex"].lower()  or s in i["Tel"].lower(): 
                k+=1 
                id_list.append(pb.pblist.index(i)) 
        print "\nThere are %s results:\n"% k 
        #print id_list 

            print "*******************************************" 
        print "  ID     NAME     SEX      TEL" 
        print "*******************************************" 
        for j in id_list: 
            print "%3s     " % str(j+1), 
            print "%-8s" % pb.pblist[j]["Name"], 
                    print "%-8s" % pb.pblist[j]["Sex"], 
            print "%-8s" % pb.pblist[j]["Tel"] 
                print "==========================================="        

                print ("\nContinue?(Y/N)\n") 
        while 1: 
                       con=raw_input() 
            if con=="Y": 
                            pb_search() 
                    elif  con=="N": 
                            main() 
            else: 
                print ("\nError! Continue?(Y/N)\n")

def pb_file(): 
        #print "This is the file function" 
        try: 
                f=file(pbfile) 
                pb.pblist=p.load(f) 
        except: 
                pb.list=[] 
        finally: 
                f.close() 

def pb_save(): 
        #print "This is the SAVE function" 
        try: 
                f=file(pbfile,"w") 
                p.dump(pb.pblist,f) 
                print "\nRecords saved successfully!\n" 
        finally: 
                f.close() 

def pb_quit(): 
        print "Thanks for your using!" 
        exit() 

def main(): 
        print """\ 
____________________________________________________  
|       Please select your choice:                 | 
|__________________________________________________| 
|       [A]: Add a record.                         | 
|       [B]: Browser the records.                  | 
|       [D]: Delete a record.                      | 
|       [S]: Search.                               | 
|       [Q]: Quit.                                 | 
|__________________________________________________| 
        \ 
        """ 
        while 1: 
                opp=raw_input() 
                if opp=="A": 
                        pb_add() 
                if opp=="B": 
                        pb_browser() 
                if opp=="D": 
                        pb_del() 
                if opp=="S": 
                        pb_search() 
                if opp=="Q": 
                        pb_quit() 

pb=phonebook() 
pbfile=("pb.dat") 
pb_file() 

main()
2009年5月18日 0 评论 344 阅读
加载更多

热门

  • 无线餐饮点菜系统–去年的毕业设计

    2008年9月21日 36 评论 1.7k 阅读
  • Magento设置Paypal支付步骤

    2011年5月29日 1 评论 973 阅读
  • 垃圾

    2017年3月12日 0 评论 822 阅读
  • 记录

    2007年10月6日 0 评论 811 阅读
  • 办公室的两只猫

    2017年12月19日 0 评论 808 阅读

最新

  • 今天来了公司

    2023年1月27日 0 评论 8 阅读
  • 我觉得留在这个世界上,真没有意思

    2023年1月26日 0 评论 11 阅读
  • 世界还有好多美好的事,伯伯却看不到了

    2023年1月22日 0 评论 31 阅读
  • 我好想死

    2023年1月20日 0 评论 20 阅读
  • 对不起

    2023年1月20日 0 评论 23 阅读

分类

  • 图片 (71)
  • 外贸 (6)
  • 大学 (103)
  • 技术 (75)
  • 杂志 (290)
  • 电影 (13)
  • 资源 (1)
  • 随语 (61)
  • 音乐 (96)
  • 饮食 (3)

归档

  • 2023年1月 (27)
  • 2021年8月 (2)
  • 2021年1月 (1)
  • 2020年3月 (1)
  • 2019年3月 (1)
  • 2017年12月 (2)
  • 2017年3月 (2)
  • 2011年10月 (3)
  • 2011年8月 (1)
  • 2011年7月 (1)
  • 2011年5月 (1)
  • 2011年4月 (1)
  • 2011年1月 (2)
  • 2010年12月 (1)
  • 2010年9月 (2)
  • 2010年8月 (5)
  • 2010年7月 (1)
  • 2010年6月 (1)
  • 2010年5月 (2)
  • 2010年4月 (3)
  • 2010年2月 (4)
  • 2010年1月 (9)
  • 2009年12月 (3)
  • 2009年11月 (3)
  • 2009年9月 (11)
  • 2009年8月 (6)
  • 2009年7月 (1)
  • 2009年6月 (2)
  • 2009年5月 (7)
  • 2009年4月 (13)
  • 2009年3月 (14)
  • 2009年2月 (2)
  • 2009年1月 (12)
  • 2008年12月 (19)
  • 2008年11月 (1)
  • 2008年10月 (9)
  • 2008年9月 (12)
  • 2008年8月 (5)
  • 2008年7月 (7)
  • 2008年6月 (13)
  • 2008年5月 (4)
  • 2008年4月 (10)
  • 2008年3月 (7)
  • 2008年2月 (7)
  • 2008年1月 (11)
  • 2007年12月 (4)
  • 2007年11月 (8)
  • 2007年10月 (30)
  • 2007年9月 (25)
  • 2007年8月 (29)
  • 2007年7月 (32)
  • 2007年6月 (2)
  • 2007年4月 (1)
  • 2007年3月 (2)
  • 2007年2月 (5)
  • 2007年1月 (7)
  • 2006年12月 (11)
  • 2006年11月 (24)
  • 2006年10月 (13)
  • 2006年9月 (16)
  • 2006年8月 (21)
  • 2006年7月 (18)
  • 2006年6月 (9)
  • 2006年5月 (27)
  • 2006年4月 (44)
  • 2006年3月 (21)
  • 2006年2月 (9)
  • 2006年1月 (6)
  • 2005年12月 (12)
  • 2005年11月 (17)
  • 2005年10月 (44)
  • 2005年9月 (12)

标签

asp错误 (1) cache (1) django (1) joomla (8) joomla extension (3) magento (2) magento connect (1) Magento Email Templates (1) pink (1) Plone (1) python (3) QTP (1) sef (2) so what (1) stop 0x0000007b (1) Sugar In The Marmalade (1) vbs (1) virtuemart (3) Virtuemart Menu (1) Zope (1) 专升本 (4) 周慧敏 (2) 喜爱 (1) 国庆 (1) 域名注册,域名 (1) 学生处 (6) 我爱谁 (1) 我的大学 (6) 我的大学编年史 (6) 拉肚子 (1) 樊海军 (6) 物理学系 (6) 物理系 (4) 电子信息工程 (4) 简单电话薄 (1) 聚会 (1) 肇庆 (4) 肇庆学院 (6) 计算机系 (4) 谭咏麟 (3) 软件测试 (4) 邓奎元,蔡金生,喝酒 (1) 酸 (1) 黎明 (2) 黎明 对不起 (1)
  • 首页
  • 分类
    • 随语
    • 杂志
    • 外贸
    • 技术
    • 图片
    • 电影
    • 音乐
    • 饮食
    • 大学
  • 关于

@2005-2023 - 白菜粉条汤

白菜粉条汤
  • 随语
  • 杂志
  • 资源
  • 外贸
  • 技术
  • 图片
  • 电影
  • 音乐
  • 饮食
  • 大学
登入

保持登录状态,直到我退出

忘记密码了吗?

找回密码

新密码将通过电子邮件发送给您。

收到新密码了吗? Login here