zoukankan      html  css  js  c++  java
  • python通讯录小程序

    最近看《A Byte of Python》自学python,看完全书之后书最后面有习题,说如果做出来这个程序就可以自称为python程序员^ ^

    然后我就随便写了一下,发现bug真多,哈哈,小程序,代码也帖出来吧

    1 # com.address
    2 # Filename: __init__.py
    3 # 先建一个字典,来存通讯录数据
    4 class person:
    5     infomation = {'name':'number'}
    6 
    7 #print person.infomation
     1 #com.address
     2 #Filename: zsgc_address
     3 
     4 from __init__ import person
     5 import cPickle as p
     6 
     7 numberfile = 'numberfile.data'
     8 f = file(numberfile)
     9 # choose
    10 while True:
    11     choose = str(raw_input('''what do u what? 
    12 enter '1' read 
    13 enter '2' add 
    14 enter '3' del
    15 enter 'exit' out  ''')) 
    16 #read
    17     if choose == '1':
    18         if person.infomation == {}:
    19             print 'nothing here, u can add the first address'
    20         else:
    21             f = file(numberfile, 'r')
    22             person.infomation = p.load(f)
    23             for name, number in person.infomation.items():
    24                 print 'name is %s ,number is %s' % (name, number)
    25 
    26 #write
    27     elif choose == '2':
    28 #input   
    29         add_name = str(raw_input('''input the add_name : '''))
    30         add_number = str(raw_input('''input the add_number: '''))
    31         person.infomation[add_name] = add_number
    32 #cPickle to data    
    33         f = file(numberfile, 'w')
    34         p.dump(person.infomation, f)
    35         print 'newone name is %s ,number is %s' % (add_name, add_number)
    36 
    37 #delete    
    38     elif choose == '3':
    39         if person.infomation == {}:
    40             print 'nothing here to be delete'
    41             continue
    42         else:
    43             f = file(numberfile, 'w')
    44             del_name = str(raw_input('''input the person's name 
    45             that u gonna to del : '''))
    46             del person.infomation[del_name]
    47             p.dump(person.infomation, f)
    48             print 'delete %s ok! ' % del_name
    49 
    50 #out
    51     elif choose == 'exit':
    52         print 'byebyebyebyebyebyebye'
    53         f.close()
    54         break
    55 
    56 #others        
    57     elif len(choose) == 0:
    58         print 'please enter something'
    59         
    60     else:
    61         print 'please make a choose'
    62         continue
    63     
  • 相关阅读:
    socket选项设置
    shell 备忘录
    VIM中cscope和tags数据库的添加
    MFC程序设计中的BeginPaint/EndPaint和GetDC/ReleaseDC的使用
    shell 命令行参数解析
    do{...}while(0)用法总结
    0长度数组的使用
    在线帮助文档
    GCC编译器帮助文档
    几款优秀的Linux基准测试工具
  • 原文地址:https://www.cnblogs.com/vincentvan/p/2872197.html
Copyright © 2011-2022 走看看