<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 97,  comments - 93,  trackbacks - 0
    這兩天在學習python,主要參考簡明教程http://www.swaroopch.com/byteofpython/ , 入門很不錯,有興趣推薦參考。
    在16章中,作者建議讀者寫一個地址簿程序,還記得寫它都是在大一學c語言的年代了,不過下午還是抽出了一點時間來寫這個程序,寫了兩個版本,一個是使用python 字典來存儲名字和地址,另一個版本則使用python的面向對象特性,來鏈式存儲聯系人對象。
    版本一代碼:
      1import cPickle as p,sys,re
      2
      3# Author:nicky(nicky.jcoder@gmail.com)
      4# July 24th,2008
      5
      6def readAdd():
      7    f = file(ftxt)
      8    map = p.load(f)
      9    f.close()
     10    return map
     11
     12def autoSave():
     13    f=file(ftxt,'w')
     14    p.dump(book,f)
     15    f.close()
     16    
     17def run():
     18   while True:
     19      try:
     20         com = int(raw_input(welcome+features))
     21      except:
     22         print "\ninput error"
     23         run()
     24      if com == 1:
     25          info = raw_input("input the name and address:")
     26          infoList = re.split(' ',info)
     27          add(infoList[0],infoList[1])
     28          run()
     29          autoSave()
     30          book = readAdd()
     31      elif com == 2:
     32          info = raw_input("input the name:")
     33          search(info)
     34          run()
     35          autoSave()
     36          book = readAdd()
     37      elif com == 3:
     38          info = raw_input("input the name and address:")
     39          infoList = re.split(' ',info)
     40          modify(infoList[0],infoList[1])
     41          run()
     42          autoSave()
     43          book = readAdd()        
     44      elif com == 4:
     45          info = raw_input("input the name:")
     46          delete(info)
     47          run()
     48          autoSave()
     49          book = readAdd()
     50      elif com ==5:
     51          print 'All the people and their addresses are listed:'
     52          browseAll()
     53      elif com == 6:
     54          autoSave()
     55          sys.exit()
     56      else:
     57          print 'your input is invalid'
     58          run()
     59
     60def add(people,address):
     61    if people not in book:
     62        book[people]=address
     63        print '\nadd successfully'
     64    else:
     65        print '\nthe '+people+' exists in the address book'
     66        
     67def search(people):
     68    if people in book:
     69        print "\n"+book[people]
     70    else:
     71        print '\nthe'+people+' is not in the address book'
     72
     73def modify(people,address):
     74    if people in book:
     75        book[people]=address
     76        print '\nmodify successfully'
     77    else:
     78        print '\nthe'+people+' is not in the address book'
     79
     80def delete(people):
     81    if people in book:
     82        del book[people]
     83        print '\ndelete successfully'
     84    else:
     85        print '\nsome errors has been arised'
     86
     87def browseAll():
     88    for people,address in book.items():
     89        print people+':'+address
     90
     91def judgeAddressbook():
     92    f = file(ftxt,'w')
     93    count =0
     94    while True:
     95        line=f.readline()
     96        if len(line)==0:
     97         break
     98        count=count+1
     99    if count == 0:
    100        book={'test':'test'}
    101        autoSave()
    102
    103if __name__ == '__main__':
    104  welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
    105  features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
    106  ftxt = 'addressbook.txt'
    107  book={}
    108  judgeAddressbook()    
    109  book=readAdd()
    110  run()
    版本二代碼:
      1import cPickle as p,sys,re
      2
      3# Author:nicky(nicky.jcoder@gmail.com)
      4# July 24th,2008
      5
      6def readAll():
      7    f = file(ftxt)
      8    list = p.load(f)
      9    f.close()
     10    return list
     11
     12def autoSave():
     13    f=file(ftxt,'w')
     14    p.dump(book,f)
     15    f.close()
     16 
     17class People:
     18    def __init__(self,name,address):
     19        self.name=name
     20        self.address=address
     21    def getName(self):
     22        return self.name
     23    def getAddress(self):
     24        return self.address
     25    def setAddress(address):
     26        self.address=address
     27        
     28def run():
     29   while True:
     30      try:
     31         com = int(raw_input(welcome+features))
     32      except:
     33         print "\ninput error"
     34         run()
     35      if com == 1:
     36          info = raw_input("input the name and address:")
     37          infoList = re.split(' ',info)
     38          add(infoList[0],infoList[1])
     39          run()
     40          autoSave()
     41          book = readAdd()
     42      elif com == 2:
     43          info = raw_input("input the name:")
     44          search(info)
     45          run()
     46          autoSave()
     47          book = readAdd()
     48      elif com == 3:
     49          info = raw_input("input the name and address:")
     50          infoList = re.split(' ',info)
     51          modify(infoList[0],infoList[1])
     52          run()
     53          autoSave()
     54          book = readAdd()        
     55      elif com == 4:
     56          info = raw_input("input the name:")
     57          delete(info)
     58          run()
     59          autoSave()
     60          book = readAdd()
     61      elif com ==5:
     62          print 'All the people and their addresses are listed:'
     63          browseAll()
     64      elif com == 6:
     65          autoSave()
     66          sys.exit()
     67      else:
     68          print 'your input is invalid'
     69          run()
     70
     71def add(people,address):
     72    flag = -1
     73    for item in book:
     74        if people != item.getName():
     75            instance = People(people,address)
     76            book.append(instance)
     77            print '\nadd successfully'
     78            flag =0
     79    if flag == -1:
     80      print '\nthe '+people+' exists in the address book'
     81        
     82def search(people):
     83    flag=-1
     84    for item in book:
     85      if people == item.getName():
     86          print "\n"+item.getAddress()
     87          flag=0
     88    if flag ==-1:
     89        print '\nthe'+people+' is not in the address book'
     90
     91def modify(people,address):
     92    flag=-1
     93    for item in book:
     94       if people == item.getName:
     95               item.setAddress(address)
     96               flag=0
     97               print '\nmodify successfully'
     98    if flag==-1:
     99      print '\nthe'+people+' is not in the address book'
    100       
    101
    102def delete(people):
    103    flag =-1
    104    for item in book:
    105        if people == item.getName:
    106            del item
    107            flag=0
    108            print '\ndelete successfully'
    109    if flag ==-1:
    110       print '\nsome errors has been arised'
    111        
    112
    113def browseAll():
    114    for item in book:
    115        print item.getName()+":"+item.getAddress()
    116
    117def judgeAddressbook():
    118    f = file(ftxt,'w')
    119    count =0
    120    while True:
    121        line=f.readline()
    122        if len(line)==0:
    123         break
    124        count=count+1
    125    if count == 0:
    126        instance=People("test1","test1")
    127        book.append(instance)
    128        autoSave()
    129
    130if __name__ == '__main__':
    131  welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
    132  features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
    133  ftxt = 'addressbook_object.txt'
    134  book=[]
    135  judgeAddressbook()    
    136  book=readAll()
    137  run()

    運行方法應該不用說了
    python 模塊
    效果圖為

     

    posted on 2008-07-24 17:51 wqwqwqwqwq 閱讀(1562) 評論(3)  編輯  收藏 所屬分類: python

    FeedBack:
    # re: python地址簿程序(過程存儲,對象存儲)[未登錄]
    2008-10-19 08:57 | LJ
    你好,想請教個問題,我運行了你的第一個python地址簿程序,輸入6退出后,再進入,就會從新建立一個新的空白的addressbook.txt文檔,覆蓋了之前那個已經編輯好的,怎么解決這個問題呢?謝謝!  回復  更多評論
      
    # re: python地址簿程序(過程存儲,對象存儲)
    2008-10-19 09:23 | nickyqu
    主程序中
    #book={}
    #judgeAddressbook()
    具體的對其它的影響我沒細看,add容錯沒加,你可以加進去。  回復  更多評論
      
    # Blogosfera
    2009-05-18 08:43 | Blogosfera
    Could you help me. Good judgment comes from experience, and experience comes from bad judgment.
    I am from Ghana and also now am reading in English, tell me right I wrote the following sentence: "Seo design uses web design and search engine optimization to form a superior ecommerce shopping cart.I offer free seo services to small and mid sized companies."

    Thanks for the help :-D, Kalil.  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術網站

    java技術相關

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網網絡電話,中國最優秀的網絡電話
    主站蜘蛛池模板: 亚洲人成电影网站色| jizz免费一区二区三区| 四虎影视免费永久在线观看| 免费精品久久久久久中文字幕| 日韩亚洲人成在线综合日本| 美女网站免费福利视频| 美女视频黄频a免费观看| 亚洲A∨无码一区二区三区| 最近中文字幕无吗高清免费视频| 一级特黄特色的免费大片视频| 少妇中文字幕乱码亚洲影视| 日韩激情无码免费毛片| 男人进去女人爽免费视频国产| 亚洲女子高潮不断爆白浆| 亚洲gv猛男gv无码男同短文| 国产成人高清精品免费软件 | 美国免费高清一级毛片| 亚洲gv猛男gv无码男同短文| 国产成人免费a在线视频app| 99热精品在线免费观看| 一级女性全黄生活片免费看| 456亚洲人成影院在线观| 中文字幕久久亚洲一区| 午夜爱爱免费视频| 久久久免费的精品| 亚洲黄片手机免费观看| 亚洲一区AV无码少妇电影| 亚洲A∨无码无在线观看| 亚洲午夜日韩高清一区| 成年美女黄网站18禁免费| 久久久久国产精品免费免费不卡| 男女污污污超污视频免费在线看 | 99免费精品视频| 老湿机一区午夜精品免费福利| 亚洲人成图片网站| 久久亚洲春色中文字幕久久久 | 亚洲黄色免费观看| 亚洲夜夜欢A∨一区二区三区| 国产免费av一区二区三区| 青青草免费在线视频| 国产精品成人观看视频免费|