Dear friends,

I am a new programmer and i have a task to make a phonebook with python. Please help me to finish my task.

The program should repeatedly ask the user for input. Each primary input is one of the words
“help”, “lookup”, “enter”, “remove”, and “quit”. If the user types in something else, the
program should show help. For some primary inputs, the program asks secondary input:
help: no secondary input; the program just prints information about available
commands and their inputs
lookup: secondary input is a name; the (final) program looks up the number for that name
(if any)
enter: secondary input consists of a name and a phone number; the (final) program
registers the number with the name
remove: secondary input is a name; the (final) program removes the name (if any) and
corresponding number
quit: no secondary input; the program ends
The actual functions of the final program will be filled in during the next block of
assignments. To prepare for the next version of the program, supply the secondary input to these functions now. Also, each function (except for help) will need a parameter for the phonebook, so supply it here already – for now, use a variable in the program that is
initialized to “{}” (a pair of curly braces, i.e. an empty dictionary).
Add persistence to the phonebook program by using module shelve. “Persistence” means that
data is retained also when the program stops executing.
Objects created by opening a “shelve” behave largely like dictionaries. So we may replace the dictionary by a “shelve”, and the functions that alter the phonebook should still work.
The only differences are in the main program. At the start of the program, open the “shelve” object – if no corresponding file is present, module shelve creates an empty “shelve”. At the end of the program, close the “shelve” object again.

Thank you for your attention. I am waiting for the reply as soon as possible. I stuck with this task.

So your main program is a loop that accepts user input using either raw_input or if Python 3, input. Assuming Python 2.x, then it looks like:

keep_asking = True
prompt = 'Choose from “help”, “lookup”, “enter”, “remove”, and “quit”: '
while keep_asking:
  user_input = raw_input(prompt)
  # do something with user_input

I have used prompt variable so you can easily change it if you want/need to
I have used keep_asking variable so you can set it False in the part of the code that handles the 'quit' command
I have deliberately not done any of the 'real' code: You need to try to do that for yourself, and ask specific questions here about specific problems. Be sure to use the (CODE) button to put (CODE) and (/CODE) tags around your code so we can see indentation, keyword colors, and line numbers.

Thank you griswolf for your reply.

prompt = ('command (help/enter/lookup/remove/quit)?')
book = {}
running = True
while running:
    command = raw_input(prompt)
    if command == 'help':
        show_help()
    elif command == 'quit':
        running = False
    elif command == 'enter':
        name = raw_input('name? ')
        number = raw_input('number? ')
        enter(book, name, number)
    elif command == 'lookup':
        name = raw_input('name? ')
        lookup(book, name)
    elif command == 'remove':
        name = raw_input('name? ')
        remove(book, name)
        
def show_help():
    print 'help'
def enter(book, name, number):
    print 'enter', name, number
def lookup(book, name):
    print 'lookup', name
def remove(book, name):
    print 'remove', name

Any advice please?

Several things:

  1. That looks (at least approximately) correct.
  2. I would refactor the 'inner' raw_input to be inside the function to which you dispatch: The main/dispatch loop doesn't need to know what the functions do.
  3. if/elif/else is not the most pythonic way to handle a 'big switch statement' such as this. Instead, use a dictionary of keys and (callback) functions. Look here (and read on down)
  4. You will want a default branch that handles unexpected input
  5. In the real world you would need an 'edit' option too, though 'remove' followed by 'enter' is enough for a toy program
  6. In the real world you would want some way to save the data for later use. You need to think about how to do that: Is it automatic or is there a 'save' or 'save as' option?
  7. Related to save: Is there a 'load' option? Can you load a different phone book if you want, or only the default?

edit: I see there is something called 'shelve' that will handle my last two points. OK. If you have access to code for things like that, you can learn a lot by looking at 'real world' code (It is how I started to learn programming).

I wonder if it is possible to poste your hole program?
I is new in the python programming language, and I want to learn the concept to using classes and more.

I mean the whole program of yours.

I did not write a whole program, but looked at darma001's and made some comments. Please learn Python by reading a book or two and doing the exercises. Or look at the online tutorials which are very good. You may also find good things at the Python Wiki. You might also like this: Google is your friend!

reading Byte of Python?
That book's not too friendly to new programmer, more friendly for programmers switching from another language.

i suggest Think Python, as it's probably the best programming intro book out there (designed for year 1 college!)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.