簡單的電話本,支持添加和查詢,python練手

# -*- 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 '找不到!'
相關文章
相關標籤/搜索