<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
    這兩天在學(xué)習(xí)python,主要參考簡明教程http://www.swaroopch.com/byteofpython/ , 入門很不錯,有興趣推薦參考。
    在16章中,作者建議讀者寫一個地址簿程序,還記得寫它都是在大一學(xué)c語言的年代了,不過下午還是抽出了一點時間來寫這個程序,寫了兩個版本,一個是使用python 字典來存儲名字和地址,另一個版本則使用python的面向?qū)ο筇匦裕瑏礞準(zhǔn)酱鎯β?lián)系人對象。
    版本一代碼:
      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()

    運行方法應(yīng)該不用說了
    python 模塊
    效果圖為

     

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

    FeedBack:
    # re: python地址簿程序(過程存儲,對象存儲)[未登錄]
    2008-10-19 08:57 | LJ
    你好,想請教個問題,我運行了你的第一個python地址簿程序,輸入6退出后,再進(jìn)入,就會從新建立一個新的空白的addressbook.txt文檔,覆蓋了之前那個已經(jīng)編輯好的,怎么解決這個問題呢?謝謝!  回復(fù)  更多評論
      
    # re: python地址簿程序(過程存儲,對象存儲)
    2008-10-19 09:23 | nickyqu
    主程序中
    #book={}
    #judgeAddressbook()
    具體的對其它的影響我沒細(xì)看,add容錯沒加,你可以加進(jìn)去。  回復(fù)  更多評論
      
    # 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.  回復(fù)  更多評論
      

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


    網(wǎng)站導(dǎo)航:
     
    <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789




    常用鏈接

    留言簿(10)

    隨筆分類(95)

    隨筆檔案(97)

    文章檔案(10)

    相冊

    J2ME技術(shù)網(wǎng)站

    java技術(shù)相關(guān)

    mess

    搜索

    •  

    最新評論

    閱讀排行榜

    校園夢網(wǎng)網(wǎng)絡(luò)電話,中國最優(yōu)秀的網(wǎng)絡(luò)電話
    主站蜘蛛池模板: a级片免费在线观看| 亚洲国产成人精品无码一区二区 | 黄页视频在线观看免费| 亚洲欧美综合精品成人导航| 国内精品久久久久影院亚洲 | 亚洲一区视频在线播放| 亚洲av无码专区在线观看素人| 国产hs免费高清在线观看| 国产在线19禁免费观看| 亚洲Aⅴ无码一区二区二三区软件| 亚洲av无码不卡私人影院| 亚洲高清偷拍一区二区三区| 国产成人亚洲综合无码| 亚洲国产精品无码专区| 亚洲激情视频在线观看| 亚洲人成综合在线播放| 亚洲av乱码中文一区二区三区| 国内成人精品亚洲日本语音| 在线播放国产不卡免费视频| 成人网站免费大全日韩国产| 久久久久久AV无码免费网站| 免费福利网站在线观看| 国产成人免费一区二区三区| 自拍偷自拍亚洲精品被多人伦好爽| 亚洲国产精品一区二区成人片国内 | 午夜网站在线观看免费完整高清观看| 97国产在线公开免费观看| 福利免费观看午夜体检区| 国产精品久免费的黄网站| 日本黄页网站免费| 亚洲午夜av影院| 久久亚洲中文字幕精品有坂深雪| 亚洲人成在线播放| 粉色视频免费入口| 最近免费mv在线观看动漫| 国产92成人精品视频免费| 在线不卡免费视频| 国产亚洲av人片在线观看| 亚洲视频在线视频| 国产成人不卡亚洲精品91| a级毛片在线免费观看|