一个简单的电话本程序,支持添加和查找功能。

# -*- coding: cp936 -*-
while True:
    ask=raw_input('欢迎使用电话本:-) 添加(A)/查找(S)?')
    if ask=='A' or ask=='a':
        while True:
            print '开始添加...'
            name=raw_input('名字: ')
            phonebook=open('myphonebookdata')
            flag=0
            for eachLine in phonebook:
                key=eachLine.split()
                if key[0]==name:
                    flag=1
                    phonebook.close()
                    break
            if flag==1:
                print '名字已经存在,请重新命名...'
            else:
                phonebook.close()
                break
        number=raw_input('电话号码: ')
        phonebook=open('myphonebookdata','a')
        phonebook.write(name+'\t'+number+'\n')
        print '添加成功!'
        phonebook.close()
    elif ask=='S' or ask=='s':
        print '开始查找...'
        name=raw_input('名字: ')
        phonebook=open('myphonebookdata')
        flag=0
        for eachLine in phonebook:
            key=eachLine.split()
            if key[0]==name:
                flag=1
                print '名字\t电话号码'
                print eachLine,
                print '查找完成!'
                phonebook.close()
                break
        if flag==0:
            print '找不到!'

一个简单的电话本程序,支持添加和查找功能。_第1张图片

你可能感兴趣的:(python,查找,添加,电话本)