473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 27374
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>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.