Thursday, February 16, 2017

Telephone Database (Persistent Variables) Python

Tags

Following Code will help in maintaining a telephone database including following commands

1.whois phonenumber -tell the details of a particular Number

2. add phonenumber information-add the new number with the information

3.search keyword-return all numbers having keyword in their information

4.delete-deleting any number from database

5.quit-Halt the application

Code::

import string
import shelve

database=shelve.open("information.db")
print("Commands are: whois,add,search,delete or quit")
c=0
line=input("Command: ")
while line!='quit':
    words=line.lower().split()
    if line=="":
        print("Commands are: whois,add,search,delete or quit")
    elif words[0] =='whois':
        if words[1] in database:
            print(words[1],":",string.capwords(database[words[1]]))
        else:
            print("Not Found Try again!!")
    elif words[0]=='add':
        if words[1] in database:
            print("Already Present")
        else:
            database[words[1]]=" ".join(words[2:])
    elif words[0]=='search':
        for e in database:
            if database[e].find(words[1])!=-1:
                c=1
                print(e,":",string.capwords(database[e]))
                
        if c==0:
            print("Keyword Not found Try again!!")
    elif words[0]=="delete":
        if words[1] in database:del database[words[1]]
        else:print("Not Found Try again!!")
    else:
        print("Command Not Found Please Try again!!")
    line=input("Command: ")
database.close()

Output::