467,915 Members | 1,146 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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'

Expand|Select|Wrap|Line Numbers
  1. if __name__ == "__main__":
  2. main()
  3.  
  4. filename = "addbook.dat"
  5.  
  6. def main():
  7. theMenu = '''
  8. 1) Add Entry
  9. 2) Remove Entry
  10. 3) Find Entry
  11. 4) Quit and Save
  12. '''
  13. theBook = {}
  14. readBook(theBook)
  15. choice = getChoice(theMenu)
  16. while choice != 4:
  17. if choice == 1:
  18. addEntry(book)
  19. elif choice == 2:
  20. removeEntry(book)
  21. elif choice == 3:
  22. findEntry(book)
  23. else:
  24. print "Invalid choice, try again."
  25. choice = getChoice(theMenu)
  26. saveBook(theBook)
  27.  
  28.  
  29. def readbook(book):
  30. import os
  31. if os.path.exists(filename):
  32. store = open(filename,'r')
  33. for line in store:
  34. name = line.rstrip()
  35. entry = store.net().rstrip
  36. book[name] = entry
  37. store.close()
  38.  
  39. def saveBook(book):
  40. store = open(filename,'w')
  41. for name,entry in book.items():
  42. store.write(name + '\n')
  43. store.write(entry + '\n')
  44. store.close()
  45.  
  46. def getChoice(menu):
  47. print menu
  48. choice = int( raw_input("Select a choice(1-4): ") )
  49. return choice
  50.  
  51. def addEntry(book):
  52. name=raw_input("Enter a name: ")
  53. entry = raw_input("Enter a street, town and phone number: ")
  54. book[name] = entry
  55.  
  56. def removeEntry(book):
  57. name=raw_input("Enter a name: ")
  58. del(book[name])
  59.  
  60. def findEntry(book):
  61. name = raw_input("Enter a name: ")
  62. if name in book:
  63. print name, book[name]
  64. else: print "Sorry, no entry for: ",name
  65.  
  66.  
  67.  
Dec 19 '07 #1
  • viewed: 19210
Share:
2 Replies
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

Dec 19 '07 #2
thanks....
Dec 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by willkab6 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.