创建一个简单的电话本,支持添加和查询功能,创建查询,# -*- coding


# -*- 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 '找不到!'#该片段来自于http://byrx.net

评论关闭