473,890 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 27750
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
23767
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 planning to then create a program to detect input and use this module, but I am hung up. I have constructed the code that I thought would work (though there may be a better way) and I got this error message. Traceback (most recent call last): File...
3
6386
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: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' running = False # this causes the while loop to stop elif guess < number:
1
30173
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 "<stdin>", line 1, in <module> NameError: name 'zeros' is not defined
1
9628
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--> for Addition" print "2--> for Subtraction" print "3--> for muliplication"
1
11772
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 up at step 4.3 i.e when modifying controller to "return Response('<p>firstapp default</p>')" I am getting error of <type 'exceptions.NameError'>: global name
4
3785
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() ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf') ftp.retrbinar('RETR sn.last', open(mydpa,'wb').write) ftp.quit()
4
2973
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 child. I am getting an error within child where in one line it understands variable master.name and in the next line it gives a NameError as given here " print "Reset name now from %s to %s , oldname %s is saved in mastertrash" %...
2
44107
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 lets the user choose how many time it preforms the function and the amplitude of the function . here is the code. I'm using Vista 64 bit and Python v2.62. def compare(): for i in range(num_iterations): A = A * a * (1 - A) B = B * a *...
4
4260
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> Traceback (most recent call last): File "./Main.py", line 7, in <module> print (t.getAtomName()) File "/home/jorge/Documentos/projetos/mestrado/códigos/cartesian_zmatrix/Atom.py", line 22, in getAtomName return atomName
0
11222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10810
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10918
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.