Hello i made a phonebook script on python. I am quite new and have been only learning for less than a day and i cant get it to save the entered phonenumbers (is resets to default everytime you quit) Please help me with the code please!

phonebook["test"]=12345678

def searchname():
    print ""
    name=raw_input("Whose number do you want to search:")
    if phonebook.has_key(name):
            print "The phone number of",name,"is",phonebook[name]
    else:
        print name,"is not in the phonebook"
    print ""        
    newname()

    
def newname():
    print ""
    print "What do you want to do?"
    print "1)Search another name"
    print "2)Back to main menu"
    a=input("enter what you want to do here:")
    if a==1:
        searchname()
    elif a==2:
        menu()


def adddeletecontact():
    print""
    print "What do you want to do:"
    print""
    print "1)Add an contact"
    print "2)Delete an contact"
    print "3)Back to main menu"
    cmo=input("What do you want to do:")
    if cmo==1:
        addcontact()
    elif cmo==2:
        delcontact()
    elif cmo==3:
        menu()
        
    
    


def addcontact():
    print ""
    cn=raw_input("Enter name of contact here:")
    cno=input("Enter number of contact here:")             
    phonebook[cn]=cno
    print "contact added!"
    print ""
    contactoptions()

def delcontact():
    print ""
    contactname=raw_input("Who do you want to delete:")
    if phonebook.has_key(contactname):
        del phonebook[contactname]
        print contactname,"has been successfuly deleted!"
    else:
        print name,"is not in the phonebook"
    print ""
    print contactoptions()
        
def contactoptions():
    print ""
    print "What do you want to do?"
    print "1)Add/Delete contact"
    print "2)Back to main menu"
    a=input("enter what you want to do here:")
    if a==1:
        adddeletecontact()
    if a==2:
        menu()
        
def showallcontacts():
    print ""
    print "The following people are in the phonebook:"
    print phonebook.keys()
    print""
    enter=raw_input("Press any key to go to the main menu")
    menu()


        
    
            
def menu():
    loop=1
    choice=0
    while loop==1:
        print ""
        print ""
        print "Welcome to Phonebook!"
        print "Choose your options"
        print ""
        print "1)Search a contact"
        print "2)Add or delete a contact"
        print "3)All contacts"
        print "4)Quit"
        print ""
        g=input("Enter what you want to so here:")
        if g==1:
            searchname()
        elif g==2:
            adddeletecontact()
        elif g==3:
            showallcontacts()
        elif g==4:
            loop=0
    print "Thanks for using Phonebook!"

menu()

Sorry missed out a crucial piece of code

phonebook={}

phonebook["test"]=12345678

def searchname():
    print ""
    name=raw_input("Whose number do you want to search:")
    if phonebook.has_key(name):
            print "The phone number of",name,"is",phonebook[name]
    else:
        print name,"is not in the phonebook"
    print ""        
    newname()


def newname():
    print ""
    print "What do you want to do?"
    print "1)Search another name"
    print "2)Back to main menu"
    a=input("enter what you want to do here:")
    if a==1:
        searchname()
    elif a==2:
        menu()


def adddeletecontact():
    print""
    print "What do you want to do:"
    print""
    print "1)Add an contact"
    print "2)Delete an contact"
    print "3)Back to main menu"
    cmo=input("What do you want to do:")
    if cmo==1:
        addcontact()
    elif cmo==2:
        delcontact()
    elif cmo==3:
        menu()





def addcontact():
    print ""
    cn=raw_input("Enter name of contact here:")
    cno=input("Enter number of contact here:")             
    phonebook[cn]=cno
    print "contact added!"
    print ""
    contactoptions()

def delcontact():
    print ""
    contactname=raw_input("Who do you want to delete:")
    if phonebook.has_key(contactname):
        del phonebook[contactname]
        print contactname,"has been successfuly deleted!"
    else:
        print name,"is not in the phonebook"
    print ""
    print contactoptions()

def contactoptions():
    print ""
    print "What do you want to do?"
    print "1)Add/Delete contact"
    print "2)Back to main menu"
    a=input("enter what you want to do here:")
    if a==1:
        adddeletecontact()
    if a==2:
        menu()

def showallcontacts():
    print ""
    print "The following people are in the phonebook:"
    print phonebook.keys()
    print""
    enter=raw_input("Press any key to go to the main menu")
    menu()





def menu():
    loop=1
    choice=0
    while loop==1:
        print ""
        print ""
        print "Welcome to Phonebook!"
        print "Choose your options"
        print ""
        print "1)Search a contact"
        print "2)Add or delete a contact"
        print "3)All contacts"
        print "4)Quit"
        print ""
        g=input("Enter what you want to so here:")
        if g==1:
            searchname()
        elif g==2:
            adddeletecontact()
        elif g==3:
            showallcontacts()
        elif g==4:
            loop=0
    print "Thanks for using Phonebook!"

menu()

Edited 3 Years Ago by Dani: Formatting fixed

You have brittle code by using the input for numbers, so your program crashes if input letters or empty line.

You are calling menu instead of returning from submenu, so when you quit after from main menu, you will end up in submenu. Good design of submenus is to have same key to return to main menu and to have main menu a different key to exit to avoid accidental exit from program.

It is generally clearer to use the break statement to leave from while statement instead of tweaking a variable value.

As for saving, I did not find anything connected to that to correct.

Edited 4 Years Ago by pyTony: n/a

can you code me the cpickle module for this script and also help me with the script and the mistakes i really need this please

This article has been dead for over six months. Start a new discussion instead.