NameError: name 'main' is not defined

J

jolly

hey guys,

When i try to run my code I get an error. NameError name 'main is not
defined'

Code:
if __name__ == "__main__":
    main()

filename = "addbook.dat"

def main():
    theMenu = '''
    1) Add Entry
    2) Remove Entry
    3) Find Entry
    4) Quit and Save
    '''
    theBook = {}
    readBook(theBook)
    choice = getChoice(theMenu)
    while choice != 4:
        if choice == 1:
            addEntry(book)
        elif choice == 2:
            removeEntry(book)
        elif choice == 3:
            findEntry(book)
        else:
            print "Invalid choice, try again."
        choice = getChoice(theMenu)
    saveBook(theBook)


def readbook(book):
    import os
    if os.path.exists(filename):
        store = open(filename,'r')
        for line in store:
            name = line.rstrip()
            entry = store.net().rstrip
            book[name] = entry
        store.close()

def saveBook(book):
    store = open(filename,'w')
    for name,entry in book.items():
        store.write(name + '\n')
        store.write(entry + '\n')
    store.close()

def getChoice(menu):
    print menu
    choice = int( raw_input("Select a choice(1-4): ") )
    return choice

def addEntry(book):
    name=raw_input("Enter a name: ")
    entry = raw_input("Enter a street, town and phone number: ")
    book[name] = entry

def removeEntry(book):
    name=raw_input("Enter a name: ")
    del(book[name])

def findEntry(book):
    name = raw_input("Enter a name: ")
    if name in book:
        print name, book[name]
    else: print "Sorry, no entry for: ",name
 
R

Robert Kern

jolly said:
hey guys,

When i try to run my code I get an error. NameError name 'main is not
defined'

Code:
if __name__ == "__main__":
main()[/QUOTE]

Put this at the end of the file. Currently, it is getting executed before the
code that defines main().

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top