[Tutor] addressbook program

bhaaluu bhaaluu at gmail.com
Sat Jun 28 13:53:49 CEST 2008


Hello Danny,

Part of learning to program a computer is learning how to solve problems.
I copy/pasted this code directly from the email, and tried to run it, as is.
Error messages in Python are very informative. See below.

On Sat, Jun 28, 2008 at 3:31 AM, Danny Laya <danny_laya at yahoo.com> wrote:
> Hi I am making an addressBook program now, and you know some problem, 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()

The first error I got was:

File "xyz.py", line 4
  def readBook(book)
                   ^
SyntaxError: invalid syntax

shell returned 1

Press ENTER or type command to continue

The error message pointed me to line 4
and the carot (^) pointed to the end of  def readBook(book)

Can you see the error?
You forgot a colon (:) at the end of the def funcName():

Putting a colon there eliminates that error. One down, more to go....

>
> # 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()

The next error I get is:

File "xyz.py", line 7
  store = open(filename,'r')
      ^
IndentationError: expected an indented block

shell returned 1

Press ENTER or type command to continue

What's the first thing I look at? Which line is the error on?
The error message says it's on line 7. The carot points to
store. The actual message says it is an indentation error.
Fix the indentation.
One more error eliminated.......

and so on...... Fix them one at a time, until the program works.
You must have some patience, put in some work, and maybe,
with a little luck, you can learn to program your computer!

Don't give up! Keep trying! You certainly know more than you did
when you started!

What other errors are you getting?

>
> # 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 maybe ...... I got this code from Alan Gauld tutorial
> in Handling Files part.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
Kid on Bus: What are you gonna do today, Napoleon?
Napoleon Dynamite: Whatever I feel like I wanna do. Gosh!


More information about the Tutor mailing list