NameError: name 'main' is not defined

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jolly

    NameError: name 'main' is not defined

    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
  • Robert Kern

    #2
    Re: NameError: name 'main' is not defined

    jolly wrote:
    hey guys,
    >
    When i try to run my code I get an error. NameError name 'main is not
    defined'
    >
    [code]
    if __name__ == "__main__":
    main()
    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

    Comment

    • jolly

      #3
      Re: NameError: name 'main' is not defined

      thanks....

      Comment

      Working...