472,373 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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
2 26299
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pythonnewb | last post by:
I am fairly new to programming but have some very basic Java background. I am just learning python and tried to make a module that would allow me to create a file containing an address book. I was...
3
by: willkab6 | last post by:
I am very new to both programming and Pyhton and while trying to do some practice using A byte of python an Error pops up on the IDLE shell. I am using windows XP. PLease see below. while running:...
1
by: tshravan | last post by:
Hello All, I am newbie to python. I was trying to use a=zeros(5) in python shell, but it is throwing the following exception in the shell Traceback (most recent call last): File...
1
by: manojnkumar | last post by:
#This is to add, sub, mul and div using functions def menu(): print "" print "Welcome to the calculator" print "===== These are some of the options =====" print "1-->...
1
by: Lalit | last post by:
Hi I am very new to web development. I started with Pylons. I am using http://www.rexx.com/~dkuhlman/pylons_quick_site.html as reference to create a sample web page using pylons. I got stuck...
4
by: Harry | last post by:
Hi there. I am trying to download a file(sn.last) from a public FTP server with the following code: from ftplib import FTP ftp=FTP('tgftp.nws.noaa.gov') ftp.login()...
4
by: harijay | last post by:
Hi I am new to writing module and object oriented python code. I am trying to understand namespaces and classes in python. I have the following test case given in three files runner , master and...
2
by: Geared | last post by:
Hello I'm new to python (been using it all of 2 weeks) and I'm trying to write a program that compares the result of an equation using two different starting numbers that the user chooses. It also...
4
by: jorgejch | last post by:
Hello, I've started with python (3) recently. Initialy only for scripting. Now I'm trying the object oriented bit. I'm getting the following error message <Atom.Atom object at 0x7f0b09597fd0>...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.