一个简单的python 地址簿程序

用python实现

#!/usr/bin/python
menu = ''' Address Book menu
          q - Quit
          a - Add a new person
          l - List all of the people in the address book
          f - Find all matches to a serach string eatered by the user
          r - Read previously saved data from disk
          w - Write the current contents to disk'''

print menu

people = [] # list

while(True):
    input = raw_input("Enter operation : ").strip()
    if input == 'q':
        break
    elif input == 'a':
        print "Adding a new person"
        given_name = raw_input("Enter Given Name : ").strip()
        family_name = raw_input("Enter Family Name : ").strip()
        phone = raw_input("Enter Phone Number : ").strip()
        mail = raw_input("Enter E-mail Address : ").strip()
        if not given_name:
            print "Not a valid name"
            break
        new_person = {
                    'given_name' : given_name,
                    'family_name' : family_name,
                    'phone' : phone,
                    'mail' : mail
                     }
        people.append(new_person)

    elif input == 'l':
        #print d
        for person in people:
            print "{} {}, email {}, phone {}\n".format(person['given_name'],
                                                             person['family_name'],
                                                             person['phone'],
                                                             person['mail'])
    elif input == 'f':
        look_for = raw_input("Enter keyword to search for : ").strip()
        for person in people:
            if (look_for in person['given_name'] or
                look_for in person['family_name'] or
                look_for in person['mail']):
                print "{} {}, email {}, phone {}\n".format(person['given_name'],
                                                                 person['family_name'],
                                                                 person['phone'],
                                                                 person['mail'])
    elif input == 'r':
        print "Reading previously saved data from disk"
        for line in open('address.txt', 'Ur'):
            given_name,family_name,phone,mail = line.split()
            new_person =  {'given_name' : given_name,
                           'family_name': family_name,
                           'phone' : phone,
                           'mail' : mail}
            people.append(new_person)

    elif input == 'w':
        print "Writing the current contents to disk"
        with open('address.txt', 'w') as f:
                for person in people:
                        f.write('{}\t{}\t{}\t{}\n'.format(person['given_name'],
                                                             person['family_name'],
                                                             person['phone'],
                                                             person['mail']))
        f.close()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值