Nietzsche says "God is dead" God says "Nietzsche is dead "
Header image

Phone Book – Telefon Defteri Python

Posted by ALonon in Python

C++ ile yazdığım telefon defterinin benzerini python ile yazdım.
Dict kullandığımdan dolayı aynı isimde iki kişi olmamasına dikkat edin, dilerseniz list yapısına çevirebilirsiniz.
Aynı klasörde example.txt’yi yaratmayı unutmayın ( windows için tam path girmeniz lazım.)
Surname kısmını eklemek isterseniz, pdict[name] =(phone,surname) şeklinde ekleyebilirsiniz.

This python phone book is about same with c++ one.
I used dict, so you can not add records with same name, you can use list if you want.
Don’t forget to create example.txt in same path ( for windows you have to write exect path )
if you want to add surname, you can use pdict[name] =(phone,surname)

python phone book

from pickle import dump, load
fr = open('example.txt','rb')
def add():
    fw = open('example.txt','wb')
    name=raw_input("Name ?\n")
    phone=raw_input("Phone ?\n")
    pdict[name]=phone
    dump(pdict,fw)
    fw.close()
def delete():
    fr = open('example.txt','rb')
    pdict=load(fr)
    fr.close()
    name=raw_input("Name ?\n")
    if pdict.has_key(name):
        del pdict[name]
        fw=open('example.txt','wb')
        dump(pdict,fw)
        fw.close()
    else:
         print "record can not be found"
def list():
    fr = open('example.txt','rb')
    pdict=load(fr)
    for a,b in pdict.iteritems():
        print a+" - "+b+"\n"
    fr.close()
c=0
while(c != 4):
    print "Phone Book\n1 to add \n2 to list\n3 to delete"
    c = int(raw_input("Enter your choice:"))
    if c==1:
        add()
    elif c==2:
        list()
    elif c==3:
        delete()

Related posts

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses



Leave a Reply