[Tutor] addressbook program
Danny Laya
danny_laya at yahoo.com
Sat Jun 28 12:30:50 CEST 2008
Hi I am making an addressBook program now, and you know ....... another error message , the program doesn't work. This is the code :
# Loading the addressbook
filename = "addbook.dat"
def readBook(book)
import os
if os.path.exists(filename):
store = open(filename,'r')
for line in store:
name = line.rstrip()
entry = store.next().rstrip()
book[name] = entry
store.close()
# Saving the address book
def saveBook(book):
store = open(filename,"w")
for name,entry in book.items():
store.write(name +
'\n')
store.write(entry + '\n')
store.close()
# Getting User Input
def getChoice(menu):
print menu
choice = int(raw_input("Select a choice(1-4): "))
return choice
# Adding an entry
def addEntry(book):
name = raw_input("Enter a name: ")
entry = raw_input("Enter a street, town and phone number: ")
book[name] = entry
# Removing an entry
def removeEntry(book):
name = raw_input("Enter a name: ")
del(book[name])
#Finding an Entry
def findEntry(book):
name = raw_input("Enter a name: ")
if name in book:
print name, book[name]
else: print "Sorry, no entry for: ", name
#
Quitting the program
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(theBook)
elif choice == 2:
removeEntry(theBook)
elif choice == 3:
findEntry(theBook)
else: print "Invalid choice, try again"
choice =
getChoice(theMenu)
saveBook(theBook)
# Call the main function
if __name__ == "__main__":
main()
Help me guy's .... Some enlightenment and explanation about the wrong and how this program works ...... I got this code from Alan Gauld tutorial in Handling Files part.
And o...yeah the error message is :
File "addressbook.py", line 4
def readBook(book)
^
SyntaxError: invalid syntax
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080628/35a83129/attachment.htm>
More information about the Tutor
mailing list